pike.git / lib / modules / Thread.pmod

version» Context lines:

pike.git/lib/modules/Thread.pmod:14: Inside #if constant(thread_create)
  {    return thread_create( f, @args );   }      optional constant MutexKey=__builtin.mutex_key;   optional constant Mutex=__builtin.mutex;   optional constant Condition=__builtin.condition;   optional constant _Disabled=__builtin.threads_disabled;   optional constant Local=__builtin.thread_local;    - //! @decl Thread.Thread Thread.Thread(function(mixed...:void) f, - //! mixed ... args) - //! - //! This function creates a new thread which will run simultaneously - //! to the rest of the program. The new thread will call the function - //! @[f] with the arguments @[args]. When @[f] returns the thread will cease - //! to exist. - //! - //! All Pike functions are 'thread safe' meaning that running - //! a function at the same time from different threads will not corrupt - //! any internal data in the Pike process. - //! - //! @returns - //! The returned value will be the same as the return value of - //! @[Thread.this_thread()] for the new thread. - //! - //! @note - //! This function is only available on systems with POSIX or UNIX or WIN32 - //! threads support. - //! - //! @seealso - //! @[Mutex], @[Condition], @[this_thread()] - //! +    optional constant thread_create = predef::thread_create;    - //! @decl Thread.Thread this_thread() - //! - //! This function returns the object that identifies this thread. - //! - //! @seealso - //! @[Thread.Thread()] - //! +    optional constant this_thread = predef::this_thread;    - //! @decl array(Thread.Thread) all_threads() - //! - //! This function returns an array with the thread ids of all threads. - //! - //! @seealso - //! @[Thread.Thread()] - //! +    optional constant all_threads = predef::all_threads;            //! @[Thread.Fifo] implements a fixed length first-in, first-out queue.   //! A fifo is a queue of values and is often used as a stream of data   //! between two threads.   //!   //! @note   //! Fifos are only available on systems with threads support.
pike.git/lib/modules/Thread.pmod:568: Inside #if constant(thread_create)
   static void create()    {    thread_create( dispatcher );    }   }      #else /* !constant(thread_create) */      // Simulations of some of the classes for nonthreaded use.    + /* Fallback implementation of Thread.Local */   class Local   {    static mixed data;    mixed get() {return data;}    mixed set (mixed val) {return data = val;}   }    -  + /* Fallback implementation of Thread.MutexKey */   class MutexKey (static function(:void) dec_locks)   {    int `!()    {    // Should be destructed when the mutex is, but we can't pull that    // off. Try to simulate it as well as possible.    if (dec_locks) return 0;    destruct (this_object());    return 1;    }       static void destroy()    {    if (dec_locks) dec_locks();    }   }    - //! @[Thread.Mutex] is a class that implements mutual exclusion locks. - //! Mutex locks are used to prevent multiple threads from simultaneously - //! execute sections of code which access or change shared data. The basic - //! operations for a mutex is locking and unlocking. If a thread attempts - //! to lock an already locked mutex the thread will sleep until the mutex - //! is unlocked. - //! - //! @note - //! This class is simulated when Pike is compiled without thread support, - //! so it's always available. - //! - //! In POSIX threads, mutex locks can only be unlocked by the same thread - //! that locked them. In Pike any thread can unlock a locked mutex. - //! + /* Fallback implementation of Thread.Mutex */   class Mutex   {    static int locks = 0;    static void dec_locks() {locks--;}    -  //! @decl MutexKey lock() -  //! @decl MutexKey lock(int type) -  //! -  //! This function attempts to lock the mutex. If the mutex is already -  //! locked by a different thread the current thread will sleep until the -  //! mutex is unlocked. The value returned is the 'key' to the lock. When -  //! the key is destructed or has no more references the lock will -  //! automatically be unlocked. The key will also be destructed if the lock -  //! is destructed. -  //! -  //! The @[type] argument specifies what @[lock()] should do if the -  //! mutex is already locked by this thread: -  //! @integer -  //! @value 0 (default) -  //! Throw an error. -  //! @value 1 -  //! Sleep until the mutex is unlocked. Useful if some -  //! other thread will unlock it. -  //! @value 2 -  //! Return zero. This allows recursion within a locked region of -  //! code, but in conjunction with other locks it easily leads -  //! to unspecified locking order and therefore a risk for deadlocks. -  //! -  //! @seealso -  //! @[trylock()] -  //! +     MutexKey lock (int|void type)    {    switch (type) {    default:    error ("Unknown mutex locking style: %d\n", type);    case 0:    if (locks) error ("Recursive mutex locks.\n");    break;    case 1:    break;    case 2:    if (locks)    // To be really accurate we should hang now, but somehow    // that doesn't seem too useful.    error ("Deadlock detected.\n");    }    locks++;    return MutexKey (dec_locks);    }    -  //! @decl MutexKey trylock() -  //! @decl MutexKey trylock(int type) -  //! -  //! This function performs the same operation as @[lock()], but if the mutex -  //! is already locked, it will return zero instead of sleeping until it's -  //! unlocked. -  //! -  //! @seealso -  //! @[lock()] -  //! +     MutexKey trylock (int|void type)    {    switch (type) {    default:    error ("Unknown mutex locking style: %d\n", type);    case 0:    if (locks) error ("Recursive mutex locks.\n");    break;    case 1:    case 2:    }    if (locks) return 0;    locks++;    return MutexKey (dec_locks);    }   }      #endif /* !constant(thread_create) */