source: branches/working-0711/ccl/lisp-kernel/Threads.h @ 7828

Last change on this file since 7828 was 7828, checked in by gb, 13 years ago

A little bit of Windows conditionalization.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/*
2   Copyright (C) 1994-2001 Digitool, Inc
3   This file is part of OpenMCL. 
4
5   OpenMCL is licensed under the terms of the Lisp Lesser GNU Public
6   License , known as the LLGPL and distributed with OpenMCL as the
7   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
8   which is distributed with OpenMCL as the file "LGPL".  Where these
9   conflict, the preamble takes precedence. 
10
11   OpenMCL is referenced in the preamble as the "LIBRARY."
12
13   The LLGPL is also available online at
14   http://opensource.franz.com/preamble.html
15*/
16
17#include <stdlib.h>
18#include <unistd.h>
19#include <sys/mman.h>
20#include <stdio.h>
21#include <pthread.h>
22#include <errno.h>
23#include <limits.h>
24#undef USE_MACH_SEMAPHORES
25#undef USE_POSIX_SEMAPHORES
26#ifdef DARWIN
27#define USE_MACH_SEMAPHORES 1
28#endif
29#ifndef USE_MACH_SEMAPHORES
30#define USE_POSIX_SEMAPHORES
31#include <semaphore.h>
32#endif
33#ifdef USE_MACH_SEMAPHORES
34/* We have to use Mach semaphores, even if we're otherwise
35   using POSIX signals, etc. */
36#include <mach/task.h>
37#include <mach/semaphore.h>
38#endif
39
40#include <limits.h>
41
42#ifdef FREEBSD
43#include <pthread_np.h>
44#endif
45
46#ifndef WINDOWS
47#include <sched.h>
48#endif
49
50#include "lisp.h"
51#include "lisp_globals.h"
52#include "gc.h"
53
54#ifdef USE_FUTEX
55#include <linux/futex.h>
56#include <sys/syscall.h>
57#endif
58
59#ifndef WINDOWS
60#include <syslog.h>
61#endif
62
63Boolean extern threads_initialized;
64Boolean extern log_tcr_info;
65
66#define LOCK_SPINLOCK(x,tcr) get_spin_lock(&(x),tcr)
67#define RELEASE_SPINLOCK(x) (x)=0
68
69#define TCR_TO_TSD(tcr) ((void *)((natural)(tcr)+TCR_BIAS))
70#define TCR_FROM_TSD(tsd) ((TCR *)((natural)(tsd)-TCR_BIAS))
71
72#ifdef USE_POSIX_SEMAPHORES
73typedef sem_t * SEMAPHORE;
74#define SEM_WAIT(s) sem_wait((SEMAPHORE)s)
75#define SEM_RAISE(s) sem_post((SEMAPHORE)s)
76#define SEM_BROADCAST(s, count) do {while(count) {SEM_RAISE(s);(count)--;}}while(0)
77#define SEM_TIMEDWAIT(s,t) sem_timedwait((SEMAPHORE)s,(struct timespec *)t)
78#endif
79
80#ifdef USE_MACH_SEMAPHORES
81typedef semaphore_t SEMAPHORE;
82#define SEM_WAIT(s) semaphore_wait((SEMAPHORE)(natural)s)
83#define SEM_RAISE(s) semaphore_signal((SEMAPHORE)(natural)s)
84#define SEM_BROADCAST(s,count)semaphore_signal_all((SEMAPHORE)(natural)s)
85#define SEM_TIMEDWAIT(s,t) semaphore_timedwait((SEMAPHORE)(natural)s,t)
86#endif
87
88void sem_wait_forever(SEMAPHORE s);
89
90#ifdef USE_POSIX_SEMAPHORES
91#define SEM_WAIT_FOREVER(s) sem_wait_forever((SEMAPHORE)s)
92#endif
93
94#ifdef USE_MACH_SEMAPHORES
95#define SEM_WAIT_FOREVER(s) sem_wait_forever((SEMAPHORE)(natural)s)
96#endif
97
98typedef struct
99{
100  signed_natural avail;
101  TCR* owner;
102  signed_natural  count;
103  void* signal;
104  signed_natural waiting;
105  void *malloced_ptr;
106  signed_natural spinlock;
107} _recursive_lock, *RECURSIVE_LOCK;
108
109
110int lock_recursive_lock(RECURSIVE_LOCK, TCR *);
111int unlock_recursive_lock(RECURSIVE_LOCK, TCR *);
112RECURSIVE_LOCK new_recursive_lock(void);
113void destroy_recursive_lock(RECURSIVE_LOCK);
114int recursive_lock_trylock(RECURSIVE_LOCK, TCR *, int *);
115
116#define LOCK(m, t) lock_recursive_lock((RECURSIVE_LOCK)ptr_from_lispobj(m), (TCR *)t)
117#define UNLOCK(m, t) unlock_recursive_lock((RECURSIVE_LOCK)ptr_from_lispobj(m), (TCR *)t)
118
119/* Hmm.  This doesn't look like the MacOS Thread Manager ... */
120LispObj current_thread_osid(void);
121void *current_native_thread_id(void);
122void *new_semaphore(int);
123void destroy_semaphore(void**);
124void tsd_set(LispObj, void *);
125void *tsd_get(LispObj);
126TCR *new_tcr(natural, natural);
127TCR *initial_thread_tcr;
128
129#define DEFAULT_THREAD_STACK_SIZE ((size_t) -1)
130#define MINIMAL_THREAD_STACK_SIZE ((size_t) 0)
131
132
133LispObj create_system_thread(size_t stack_size, 
134                             void* stackaddr,
135                             void* (*start_routine)(void *),
136                             void* param);
137
138TCR *get_tcr(Boolean);
139TCR *get_interrupt_tcr(Boolean);
140Boolean suspend_tcr(TCR *);
141Boolean resume_tcr(TCR *);
142
143typedef struct
144{
145  signed_natural spin; /* need spin lock to change fields */
146  signed_natural state; /* 0 = free, positive if writer, negative if readers; */
147  natural blocked_writers;
148  natural blocked_readers;
149  TCR  *writer;
150#ifdef USE_FUTEX
151  natural reader_signal;
152  natural writer_signal;
153#else
154  void * reader_signal;
155  void * writer_signal;
156#endif
157  void *malloced_ptr;
158} rwlock;
159
160
161rwlock * rwlock_new(void);
162void rwlock_destroy(rwlock *);
163int rwlock_rlock(rwlock *, TCR *, struct timespec *);
164int rwlock_wlock(rwlock *, TCR *, struct timespec *);
165int rwlock_try_wlock(rwlock *, TCR *);
166int rwlock_try_rlock(rwlock *, TCR *);
167int rwlock_unlock(rwlock *, TCR *);
168
169
170natural
171atomic_and(natural*, natural);
172
173natural
174atomic_ior(natural*, natural);
175
176#define SET_TCR_FLAG(t,bit) atomic_ior(&(t->flags),(1L<<bit))
177#define CLR_TCR_FLAG(t,bit) atomic_and(&(t->flags),~(1L<<bit))
178
179
180#ifdef SIGRTMIN
181#define SIG_SUSPEND_THREAD (SIGRTMIN+6)
182#define SIG_RESUME_THREAD (SIG_SUSPEND_THREAD+1)
183#else
184#define SIG_SUSPEND_THREAD SIGUSR1
185#define SIG_RESUME_THREAD SIGUSR2
186#endif
187
188extern int thread_suspend_signal, thread_resume_signal;
189
190void
191suspend_resume_handler(int, siginfo_t *, ExceptionInformation *);
192
193/* Maybe later
194Boolean
195rwlock_try_rlock(rwlock *);
196
197Boolean
198rwlock_try_wlock(rwlock *);
199*/
Note: See TracBrowser for help on using the repository browser.