root/ext/threads/threads.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. ScmConditionVariable
  2. ScmMutex
  3. ScmRWLock

   1 /*
   2  * threads.h - Gauche threads support
   3  *
   4  *   Copyright (c) 2000-2003 Shiro Kawai, All rights reserved.
   5  * 
   6  *   Redistribution and use in source and binary forms, with or without
   7  *   modification, are permitted provided that the following conditions
   8  *   are met:
   9  * 
  10  *   1. Redistributions of source code must retain the above copyright
  11  *      notice, this list of conditions and the following disclaimer.
  12  *
  13  *   2. Redistributions in binary form must reproduce the above copyright
  14  *      notice, this list of conditions and the following disclaimer in the
  15  *      documentation and/or other materials provided with the distribution.
  16  *
  17  *   3. Neither the name of the authors nor the names of its contributors
  18  *      may be used to endorse or promote products derived from this
  19  *      software without specific prior written permission.
  20  *
  21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  *
  33  *  $Id: threads.h,v 1.2 2003/07/05 03:29:11 shirok Exp $
  34  */
  35 
  36 #ifndef GAUCHE_THREADS_H
  37 #define GAUCHE_THREADS_H
  38 
  39 /*---------------------------------------------------------
  40  * Scheme-level thread API
  41  */
  42 
  43 extern ScmObj Scm_MakeThread(ScmProcedure *thunk, ScmObj name);
  44 extern ScmObj Scm_ThreadStart(ScmVM *vm);
  45 extern ScmObj Scm_ThreadJoin(ScmVM *vm, ScmObj timeout, ScmObj timeoutval);
  46 extern ScmObj Scm_ThreadYield(void);
  47 extern ScmObj Scm_ThreadSleep(ScmObj timeout);
  48 extern ScmObj Scm_ThreadTerminate(ScmVM *vm);
  49 
  50 /*---------------------------------------------------------
  51  * SYNCHRONIZATION DEVICES
  52  *
  53  *  Scheme-level synchrnization devices (ScmMutex, ScmConditionVariable,
  54  *  and ScmRWLock) are built on top of lower-level synchronization devices
  55  *  (ScmInternalMutex and ScmInternalCond).
  56  */
  57 
  58 /*
  59  * Scheme condition variable.
  60  */
  61 typedef struct ScmConditionVariableRec {
  62     SCM_HEADER;
  63     ScmInternalCond cv;
  64     ScmObj name;
  65     ScmObj specific;
  66 } ScmConditionVariable;
  67 
  68 SCM_CLASS_DECL(Scm_ConditionVariableClass);
  69 #define SCM_CLASS_CONDITION_VARIABLE  (&Scm_ConditionVariableClass)
  70 #define SCM_CONDITION_VARIABLE(obj)   ((ScmConditionVariable*)obj)
  71 #define SCM_CONDITION_VARIABLE_P(obj) SCM_XTYPEP(obj, SCM_CLASS_CONDITION_VARIABLE)
  72 
  73 ScmObj Scm_MakeConditionVariable(ScmObj name);
  74 ScmObj Scm_ConditionVariableSignal(ScmConditionVariable *cond);
  75 ScmObj Scm_ConditionVariableBroadcast(ScmConditionVariable *cond);
  76 
  77 /*
  78  * Scheme mutex.
  79  *    locked=FALSE  owner=dontcare       unlocked/not-abandoned
  80  *    locked=TRUE   owner=NULL           locked/not-owned
  81  *    locked=TRUE   owner=active vm      locked/owned
  82  *    locked=TRUE   owner=terminated vm  unlocked/abandoned
  83  */
  84 typedef struct ScmMutexRec {
  85     SCM_HEADER;
  86     ScmInternalMutex mutex;
  87     ScmInternalCond  cv;
  88     ScmObj name;
  89     ScmObj specific;
  90     int   locked;
  91     ScmVM *owner;              /* the thread who owns this lock; may be NULL */
  92 } ScmMutex;
  93 
  94 SCM_CLASS_DECL(Scm_MutexClass);
  95 #define SCM_CLASS_MUTEX        (&Scm_MutexClass)
  96 #define SCM_MUTEX(obj)         ((ScmMutex*)obj)
  97 #define SCM_MUTEXP(obj)        SCM_XTYPEP(obj, SCM_CLASS_MUTEX)
  98 
  99 ScmObj Scm_MakeMutex(ScmObj name);
 100 ScmObj Scm_MutexLock(ScmMutex *mutex, ScmObj timeout, ScmVM *owner);
 101 ScmObj Scm_MutexUnlock(ScmMutex *mutex, ScmConditionVariable *cv, ScmObj timeout);
 102 
 103 /*
 104  * Scheme reader/writer lock.
 105  */
 106 typedef struct ScmRWLockRec {
 107     SCM_HEADER;
 108     ScmInternalMutex mutex;
 109     ScmInternalCond cond;
 110     ScmObj name;
 111     ScmObj specific;
 112     int numReader;
 113     int numWriter;
 114 } ScmRWLock;
 115 
 116 SCM_CLASS_DECL(Scm_RWLockClass);
 117 #define SCM_CLASS_RWLOCK       (&Scm_RWLockClass)
 118 #define SCM_RWLOCK(obj)        ((ScmRWLock*)obj)
 119 #define SCM_RWLOCKP(obj)       SCM_XTYPEP(obj, SCM_CLASS_RWLOCK)
 120 
 121 ScmObj Scm_MakeRWLock(ScmObj name);
 122 
 123 
 124 #endif /*GAUCHE_THREADS_H*/

/* [<][>][^][v][top][bottom][index][help] */