implement AU host callbacks, to some extent
[ardour.git] / libs / fst / thread.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <pthread.h>
4 #include <windows.h>
5
6 typedef struct {
7     void* (*thread_function)(void*);
8     void* thread_arg;
9     pthread_t thread_id;
10     pthread_mutex_t init_lock;
11     pthread_cond_t init_cond;
12     pthread_attr_t attr;
13 } real_thread_info_t;
14
15 static DWORD WINAPI
16 fake_thread_proxy (LPVOID parameter) 
17 {
18         DWORD retval;
19         real_thread_info_t* rti = (real_thread_info_t*) parameter;
20
21         pthread_mutex_lock (&rti->init_lock);
22         rti->thread_id = pthread_self();
23         pthread_cond_signal (&rti->init_cond);
24         pthread_mutex_unlock (&rti->init_lock);
25
26 #if 0
27         if (pthread_attr_get_schedparam (&rti->attr)) {
28                 pthread_set_schedparam (pthread_self(), policy, sched_param);
29         }
30 #endif
31         /* XXX no way to use pthread API to set contention scope,
32            because that has to be done before a thread is created.
33            But ... its only meaningful for an M:N thread implemenation
34            so its not important for the only platform where
35            this code matters (Linux running Wine) because Linux
36            uses a 1:1 thread design.
37         */
38
39         retval = (DWORD) rti->thread_function (rti->thread_arg);
40         free (rti);
41
42         return retval;
43 }
44
45 int
46 wine_pthread_create (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
47 {
48         DWORD tid;
49         size_t stack_size;
50
51         real_thread_info_t* rti = (real_thread_info_t*) malloc (sizeof (real_thread_info_t));
52
53         rti->thread_function = function;
54         rti->thread_arg = arg;
55         if (attr) {
56                 rti->attr = *attr;
57         }
58
59         pthread_mutex_init (&rti->init_lock, NULL);
60         pthread_cond_init (&rti->init_cond, NULL);
61         
62         pthread_mutex_lock (&rti->init_lock);
63
64         if (attr) {
65                 if (pthread_attr_getstacksize (attr, &stack_size) != 0) {
66                         stack_size = 0;
67                 }
68         } else {
69                 stack_size = 0;
70         }
71
72         if (CreateThread (0, stack_size, fake_thread_proxy, rti, 0, &tid) == NULL) {
73                 return -1;
74         }
75
76         pthread_cond_wait (&rti->init_cond, &rti->init_lock);
77         pthread_mutex_unlock (&rti->init_lock);
78
79         *thread_id = rti->thread_id;
80
81         return 0;
82