📄 omnithread.h
字号:
// -*- Mode: C++; -*-
// Package : omnithread
// omnithread.h Created : 7/94 tjr
//
// Copyright (C) 1994,1995,1996, 1997 Olivetti & Oracle Research Laboratory
//
// This file is part of the omnithread library
//
// The omnithread library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA
//
//
// Interface to OMNI thread abstraction.
//
// This file declares classes for threads and synchronisation objects
// (mutexes, condition variables and counting semaphores).
//
// Wherever a seemingly arbitrary choice has had to be made as to the interface
// provided, the intention here has been to be as POSIX-like as possible. This
// is why there is no semaphore timed wait, for example.
//
#ifndef __omnithread_h_
#define __omnithread_h_
#ifndef NULL
#define NULL 0
#endif
class omni_mutex;
class omni_condition;
class omni_semaphore;
class omni_thread;
//
// OMNI_THREAD_EXPOSE can be defined as public or protected to expose the
// implementation class - this may be useful for debugging. Hopefully this
// won't change the underlying structure which the compiler generates so that
// this can work without recompiling the library.
//
#ifndef OMNI_THREAD_EXPOSE
#define OMNI_THREAD_EXPOSE private
#endif
//
// Include implementation-specific header file.
//
// This must define 4 CPP macros of the form OMNI_x_IMPLEMENTATION for mutex,
// condition variable, semaphore and thread. Each should define any
// implementation-specific members of the corresponding classes.
//
#if defined(__arm__) && defined(__atmos__)
#include <omnithread/posix.h>
#elif defined(__osf1__)
#include <omnithread/posix.h>
#elif defined(__aix__)
#include <omnithread/posix.h>
#elif defined(__hpux__)
#include <omnithread/posix.h>
#elif defined(__vxWorks__)
#include <omnithread/VxThread.h>
#elif defined(__WIN32__)
#if defined(__POSIX_NT__)
#include <omnithread/posix.h>
#else
#include "nt.h"
#endif
#ifdef _MSC_VER
// Using MSVC++ to compile. If compiling library as a DLL,
// define _OMNITHREAD_DLL. If compiling as a statuc library, define
// _WINSTATIC
// If compiling an application that is to be statically linked to omnithread,
// define _WINSTATIC (if the application is to be dynamically linked,
// there is no need to define any of these macros).
#if defined (_OMNITHREAD_DLL) && defined(_WINSTATIC)
#error "Both _OMNITHREAD_DLL and _WINSTATIC are defined."
#elif defined(_OMNITHREAD_DLL)
#define _OMNITHREAD_NTDLL_ __declspec(dllexport)
#elif !defined(_WINSTATIC)
#define _OMNITHREAD_NTDLL_ __declspec(dllimport)
#elif defined(_WINSTATIC)
#define _OMNITHREAD_NTDLL_
#endif
// _OMNITHREAD_DLL && _WINSTATIC
#else
// Not using MSVC++ to compile
#define _OMNITHREAD_NTDLL_
#endif
// _MSC_VER
#elif defined(__sunos__)
#if __OSVERSION__ != 5
// XXX Workaround for SUN C++ compiler (seen on 4.2) Template.DB code
// regeneration bug. See omniORB2/CORBA_sysdep.h for details.
#if !defined(__SUNPRO_CC) || __OSVERSION__ != '5'
#error "Only SunOS 5.x or later is supported."
#endif
#endif
#ifdef UseSolarisThreads
#include <omnithread/solaris.h>
#else
#include <omnithread/posix.h>
#endif
#elif defined(__linux__)
#include <omnithread/posix.h>
#elif defined(__nextstep__)
#include <omnithread/mach.h>
#elif defined(__VMS)
#include <omnithread/posix.h>
#elif defined(__SINIX__)
#include <omnithread/posix.h>
#elif defined(__osr5__)
#include <omnithread/posix.h>
#elif defined(__uw7__)
#include <omnithread/posix.h>
#elif defined(__irix__)
#include <omnithread/posix.h>
#elif defined(__freebsd__)
#include <omnithread/posix.h>
#elif defined(__rtems__)
#include <omnithread/posix.h>
#include <sched.h>
#elif defined(__darwin__)
#include <omnithread/posix.h>
#elif defined(__macos__)
#include <omnithread/posix.h>
#include <sched.h>
#else
#error "No implementation header file"
#endif
#if !defined(__WIN32__)
#define _OMNITHREAD_NTDLL_
#endif
#if (!defined(OMNI_MUTEX_IMPLEMENTATION) || \
!defined(OMNI_MUTEX_LOCK_IMPLEMENTATION) || \
!defined(OMNI_MUTEX_UNLOCK_IMPLEMENTATION) || \
!defined(OMNI_CONDITION_IMPLEMENTATION) || \
!defined(OMNI_SEMAPHORE_IMPLEMENTATION) || \
!defined(OMNI_THREAD_IMPLEMENTATION))
#error "Implementation header file incomplete"
#endif
//
// This exception is thrown in the event of a fatal error.
//
class _OMNITHREAD_NTDLL_ omni_thread_fatal {
public:
int error;
omni_thread_fatal(int e = 0) : error(e) {}
};
//
// This exception is thrown when an operation is invoked with invalid
// arguments.
//
class _OMNITHREAD_NTDLL_ omni_thread_invalid {};
///////////////////////////////////////////////////////////////////////////
//
// Mutex
//
///////////////////////////////////////////////////////////////////////////
class _OMNITHREAD_NTDLL_ omni_mutex {
public:
omni_mutex(void);
~omni_mutex(void);
inline void lock(void) { OMNI_MUTEX_LOCK_IMPLEMENTATION }
inline void unlock(void) { OMNI_MUTEX_UNLOCK_IMPLEMENTATION }
inline void acquire(void) { lock(); }
inline void release(void) { unlock(); }
// the names lock and unlock are preferred over acquire and release
// since we are attempting to be as POSIX-like as possible.
friend class omni_condition;
private:
// dummy copy constructor and operator= to prevent copying
omni_mutex(const omni_mutex&);
omni_mutex& operator=(const omni_mutex&);
OMNI_THREAD_EXPOSE:
OMNI_MUTEX_IMPLEMENTATION
};
//
// As an alternative to:
// {
// mutex.lock();
// .....
// mutex.unlock();
// }
//
// you can use a single instance of the omni_mutex_lock class:
//
// {
// omni_mutex_lock l(mutex);
// ....
// }
//
// This has the advantage that mutex.unlock() will be called automatically
// when an exception is thrown.
//
class _OMNITHREAD_NTDLL_ omni_mutex_lock {
omni_mutex& mutex;
public:
omni_mutex_lock(omni_mutex& m) : mutex(m) { mutex.lock(); }
~omni_mutex_lock(void) { mutex.unlock(); }
private:
// dummy copy constructor and operator= to prevent copying
omni_mutex_lock(const omni_mutex_lock&);
omni_mutex_lock& operator=(const omni_mutex_lock&);
};
///////////////////////////////////////////////////////////////////////////
//
// Condition variable
//
///////////////////////////////////////////////////////////////////////////
class _OMNITHREAD_NTDLL_ omni_condition {
omni_mutex* mutex;
public:
omni_condition(omni_mutex* m);
// constructor must be given a pointer to an existing mutex. The
// condition variable is then linked to the mutex, so that there is an
// implicit unlock and lock around wait() and timed_wait().
~omni_condition(void);
void wait(void);
// wait for the condition variable to be signalled. The mutex is
// implicitly released before waiting and locked again after waking up.
// If wait() is called by multiple threads, a signal may wake up more
// than one thread. See POSIX threads documentation for details.
int timedwait(unsigned long secs, unsigned long nanosecs = 0);
// timedwait() is given an absolute time to wait until. To wait for a
// relative time from now, use omni_thread::get_time. See POSIX threads
// documentation for why absolute times are better than relative.
// Returns 1 (true) if successfully signalled, 0 (false) if time
// expired.
void signal(void);
// if one or more threads have called wait(), signal wakes up at least
// one of them, possibly more. See POSIX threads documentation for
// details.
void broadcast(void);
// broadcast is like signal but wakes all threads which have called
// wait().
private:
// dummy copy constructor and operator= to prevent copying
omni_condition(const omni_condition&);
omni_condition& operator=(const omni_condition&);
OMNI_THREAD_EXPOSE:
OMNI_CONDITION_IMPLEMENTATION
};
///////////////////////////////////////////////////////////////////////////
//
// Counting semaphore
//
///////////////////////////////////////////////////////////////////////////
class _OMNITHREAD_NTDLL_ omni_semaphore {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -