a lot of VST support odds-and-ends, including preset discovery and support, extending...
[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         real_thread_info_t* rti = (real_thread_info_t*) parameter;
19
20         fprintf (stderr, "WINDOWS THREAD, @ pthread = %p\n", pthread_self());
21
22         pthread_mutex_lock (&rti->init_lock);
23         rti->thread_id = pthread_self();
24         pthread_cond_signal (&rti->init_cond);
25         pthread_mutex_unlock (&rti->init_lock);
26
27 #if 0
28         if (pthread_attr_get_schedparam (&rti->attr)) {
29                 pthread_set_schedparam (pthread_self(), policy, sched_param);
30         }
31 #endif
32         /* XXX no way to use pthread API to set contention scope,
33            because that has to be done before a thread is created.
34            But ... its only meaningful for an M:N thread implemenation
35            so its not important for the only platform where
36            this code matters (Linux running Wine) because Linux
37            uses a 1:1 thread design.
38         */
39
40         return (DWORD) rti->thread_function (rti->thread_arg);
41 }
42
43 int
44 wine_pthread_create (pthread_t* thread_id, const pthread_attr_t* attr, void *(*function)(void*), void* arg)
45 {
46         DWORD tid;
47         size_t stack_size;
48
49         fprintf (stderr, "****** Lets make a windows pthread\n");
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         fprintf (stderr, "\tset up the locks\n");
60
61         pthread_mutex_init (&rti->init_lock, NULL);
62         pthread_cond_init (&rti->init_cond, NULL);
63         
64         pthread_mutex_lock (&rti->init_lock);
65
66         fprintf (stderr, "\tget the stacksize\n");
67
68         if (attr) {
69                 if (pthread_attr_getstacksize (attr, &stack_size) != 0) {
70                         stack_size = 0;
71                 }
72         } else {
73                 stack_size = 0;
74         }
75
76         fprintf (stderr, "\tget that sucker started in the proxy, stacksize = %u\n", stack_size);
77
78         if (CreateThread (0, stack_size, fake_thread_proxy, rti, 0, &tid) == NULL) {
79                 return -1;
80         }
81
82         pthread_cond_wait (&rti->init_cond, &rti->init_lock);
83         pthread_mutex_unlock (&rti->init_lock);
84         fprintf (stderr, "\tlet it run\n");
85
86         *thread_id = rti->thread_id;
87
88         return 0;
89