/[pearpc]/src/system/osapi/posix/systhread.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/system/osapi/posix/systhread.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (12 years, 8 months ago) by dpavlin
File size: 3566 byte(s)
import upstream CVS
1 /*
2 * HT Editor
3 * systhread.cc
4 *
5 * Copyright (C) 2003 Sebastian Biallas (sb@biallas.net)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <cstdlib>
22 #include <cerrno>
23 #include <sys/time.h>
24 #include <pthread.h>
25
26 #include "system/systhread.h"
27
28 #ifdef HAVE_MACH_CLOCK_H
29 #include <mach/clock.h>
30 static mach_port_t clock_port;
31 #endif
32
33 struct sys_pthread_semaphore {
34 pthread_mutex_t mutex;
35 pthread_cond_t cond;
36 };
37
38 int sys_create_mutex(sys_mutex *m)
39 {
40 *m = (pthread_mutex_t *)malloc(sizeof (pthread_mutex_t));
41 return pthread_mutex_init((pthread_mutex_t *)*m, NULL);
42 }
43
44 int sys_create_semaphore(sys_semaphore *s)
45 {
46 *s = (sys_pthread_semaphore *)malloc(sizeof (sys_pthread_semaphore));
47 pthread_mutex_init(&((sys_pthread_semaphore*)*s)->mutex, NULL);
48 return pthread_cond_init(&((sys_pthread_semaphore*)*s)->cond, NULL);
49 }
50
51 int sys_create_thread(sys_thread *t, int flags, sys_thread_function start_routine, void *arg)
52 {
53 *t = (pthread_t *)malloc(sizeof (pthread_t));
54 return pthread_create((pthread_t *)*t, 0, start_routine, arg);
55 }
56
57 void sys_destroy_mutex(sys_mutex m)
58 {
59 pthread_mutex_destroy((pthread_mutex_t *)m);
60 free(m);
61 }
62
63 void sys_destroy_semaphore(sys_semaphore s)
64 {
65 pthread_mutex_destroy(&((sys_pthread_semaphore*)s)->mutex);
66 pthread_cond_destroy(&((sys_pthread_semaphore*)s)->cond);
67 free(s);
68 }
69
70 void sys_destroy_thread(sys_thread t)
71 {
72 // NOOP for pthreads
73 }
74
75 int sys_lock_mutex(sys_mutex m)
76 {
77 return pthread_mutex_lock((pthread_mutex_t*)m);
78 }
79
80 int sys_trylock_mutex(sys_mutex m)
81 {
82 return pthread_mutex_trylock((pthread_mutex_t*)m);
83 }
84
85 void sys_unlock_mutex(sys_mutex m)
86 {
87 pthread_mutex_unlock((pthread_mutex_t*)m);
88 }
89
90 void sys_signal_semaphore(sys_semaphore s)
91 {
92 pthread_cond_signal(&((sys_pthread_semaphore*)s)->cond);
93 }
94
95 void sys_signal_all_semaphore(sys_semaphore s)
96 {
97 pthread_cond_broadcast(&((sys_pthread_semaphore*)s)->cond);
98 }
99
100 void sys_wait_semaphore(sys_semaphore s)
101 {
102 pthread_cond_wait(&((sys_pthread_semaphore*)s)->cond, &((sys_pthread_semaphore*)s)->mutex);
103 }
104
105 void sys_wait_semaphore_bounded(sys_semaphore s, int ms)
106 {
107 struct timespec ts;
108 uint64 nsec;
109 #ifdef HAVE_MACH_CLOCK_H
110 mach_timespec_t ts2;
111 clock_get_time(clock_port, &ts2);
112 nsec = (ts2.tv_nsec + ((uint64)ms)*1000*1000);
113 ts.tv_sec = ts2.tv_sec+(uint)(nsec/1000000000);
114 ts.tv_nsec = (nsec % 1000000000ULL);
115 #else
116 clock_gettime(CLOCK_REALTIME, &ts);
117 nsec = (ts.tv_nsec + ((uint64)ms)*1000*1000);
118 ts.tv_sec = ts.tv_sec+(uint)(nsec/1000000000);
119 ts.tv_nsec = (nsec % 1000000000ULL);
120 #endif
121 pthread_cond_timedwait(&((sys_pthread_semaphore*)s)->cond, &((sys_pthread_semaphore*)s)->mutex, &ts);
122 }
123
124 void sys_lock_semaphore(sys_semaphore s)
125 {
126 pthread_mutex_lock(&((sys_pthread_semaphore*)s)->mutex);
127 }
128
129 void sys_unlock_semaphore(sys_semaphore s)
130 {
131 pthread_mutex_unlock(&((sys_pthread_semaphore*)s)->mutex);
132 }
133
134 void sys_exit_thread(void *ret)
135 {
136 pthread_exit(ret);
137 }
138
139 void *sys_join_thread(sys_thread t)
140 {
141 void *ret;
142 pthread_join(*(pthread_t *)t, &ret);
143 return ret;
144 }

  ViewVC Help
Powered by ViewVC 1.1.26