13
13
#include <stdint.h>
14
14
15
15
#include <pthread.h>
16
+ #ifdef __linux__
17
+ #define __USE_GNU
16
18
#include <dlfcn.h>
19
+ #endif
17
20
#include <semaphore.h>
21
+ #ifdef __linux__
22
+ #include <fcntl.h>
23
+ #include <signal.h>
24
+ #endif
18
25
#include <time.h>
19
26
20
27
#include "stl.h"
@@ -66,7 +73,7 @@ typedef struct _timer {
66
73
timer_t timer ; // the POSIX timer handle
67
74
char * name ;
68
75
int busy ;
69
- } mutex ;
76
+ } timer ;
70
77
#endif
71
78
72
79
// local forward defines
@@ -81,7 +88,7 @@ static thread threadlist[NTHREADS];
81
88
static mutex mutexlist [NMUTEXS ];
82
89
static semaphore semlist [NSEMAPHORES ];
83
90
#ifdef __linux__
84
- static semaphore semlist [NTIMERS ];
91
+ static timer timerlist [NTIMERS ];
85
92
#endif
86
93
static int stl_debug_flag = 1 ;
87
94
static int stl_cmdline_argc ;
@@ -111,7 +118,7 @@ stl_initialize(int argc, char **argv)
111
118
threadlist [0 ].busy = 1 ;
112
119
threadlist [0 ].name = "user" ;
113
120
threadlist [0 ].f = NULL ; // no pointer to function entry
114
- threadlist [0 ].pthread = NULL ; // it has no thread handle
121
+ threadlist [0 ].pthread = ( pthread_t ) NULL ; // it has no thread handle
115
122
}
116
123
117
124
void
@@ -441,7 +448,7 @@ stl_timer_create(char *name, double interval, int32_t semid)
441
448
LIST_LOCK
442
449
for (p = timerlist , slot = 0 ; slot < NTIMERS ; slot ++ , p ++ ) {
443
450
if (p -> busy == 0 ) {
444
- mp = p ;
451
+ tp = p ;
445
452
tp -> busy ++ ; // mark it busy
446
453
break ;
447
454
}
@@ -455,8 +462,8 @@ stl_timer_create(char *name, double interval, int32_t semid)
455
462
sevp .sigev_notify_attributes = NULL ;
456
463
457
464
// post the semaphore
458
- sevp .sigev_notify_function = stl_sem_post ;
459
- sevp .sigev . sigev_value .sival_int = semid ;
465
+ sevp .sigev_notify_function = ( void ( * )( union sigval )) stl_sem_post ;
466
+ sevp .sigev_value .sival_int = semid ;
460
467
461
468
status = timer_create (CLOCK_REALTIME , & sevp , & t );
462
469
if (status )
@@ -466,14 +473,14 @@ stl_timer_create(char *name, double interval, int32_t semid)
466
473
tp -> name = stralloc (name );
467
474
468
475
// set the interval and activate the timer
469
- struct timerspect ts ;
476
+ struct timespec ts ;
470
477
ts .tv_sec = (int )interval ;
471
478
ts .tv_nsec = (interval - ts .tv_sec ) * 1e9 ;
472
479
473
- strict itimerspec its ;
480
+ struct itimerspec its ;
474
481
its .it_interval = ts ; // interval
475
482
its .it_value = ts ; // initial value
476
- status = timer_settime (t , & ts , NULL );
483
+ status = timer_settime (t , 0 , & its , NULL );
477
484
if (status )
478
485
stl_error ("timer settime failed %s" , strerror (errno ));
479
486
@@ -540,5 +547,20 @@ stl_log(const char *fmt, ...)
540
547
static void *
541
548
get_functionptr (char * name )
542
549
{
550
+ #ifdef __linux__
551
+ // this is ugly, but I can't figure how to make dlopen/dlsym work here
552
+ FILE * fp ;
553
+ char cmd [4096 ];
554
+ void * f ;
555
+
556
+ snprintf (cmd , 4096 , "nm %s | grep %s" , stl_cmdline_argv [0 ], name );
557
+ fp = popen (cmd , "r" );
558
+ fscanf (fp , "%x" , & f );
559
+ fclose (fp );
560
+ printf ("f 0x%x\n" , f );
561
+
562
+ return f ;
563
+ #else
543
564
return (void * )dlsym (RTLD_SELF , name );
565
+ #endif
544
566
}
0 commit comments