📄 mutex.h
字号:
/*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * Copyright (C) 1999 * * This file is a part of the C++ Threads library. * * Implements mutual exclusion for threaded environment, as well as in * a shared process environment. + * @author Orn E. hansen <oe.hansen@gamma.telenordia.se> */#ifndef __THREADS_MUTEX_H#define __THREADS_MUTEX_H#include <threads/attributes.h>#include <threads/spinlock.h>#include <string>namespace cpp_threads { class WaitQueue; /** * Mutex, is a class for mutual exclusion of parallel * processes. That need to access the same memory, while * keeping exclusive access. * * @short Class for mutual exclusion of parallel processes. * @author Orn Hansen <oe.hansen@gamma.telenordia.se> */ class Mutex { public: enum mutex_kind { /** The mutex can only be locked once, by any thread. */ fast_e, /** The mutex can be locked many times, by the same thread. */ recursive_e, /** Check if a mutex is being locked by a thread holding the mutex, or if it is being unlocked when not locked */ errorcheck_e }; private: static std::string _project; int _id; WaitQueue* _waiting; scope_t _scope; struct storage { int m_magic; spinlock m_spinlock; int m_count; int m_owner; bool m_exclusive; mutex_kind m_kind; } *_m; void release(); int acquire(bool); void init(scope_t); public: Mutex(scope_t); Mutex(); ~Mutex(); /** * Each mutex variable, can exist within a single process * hierarchy or be viewed system wide. The range of the * scope is: * * <pre> * process_shared_e - interprocess * process_private_e - only within this process and its threads. * </pre> * * @return The scope of this mutex. */ scope_t scope(); /** * Try and lock the mutex, and return immediately to the * caller with the result. * * @param x Lock the mutex exclusively, write once read many lock. * @return EBUSY if the mutex is busy, EINVAL if it is an unknown kind and 0 on success. */ int tryLock(bool x=true); /** * Lock the mutex, and wait for ut if it is already * locked by another. This will suspend the calling process * until such time the lock is successful, or an error occurs. * * @param x Lock the mutex exclusively, write once read many. * @param p Priority, on the wait queue. Set to lowest by default. * @return 0 on success, EINVAL if the mutex is an unknown kind. */ int lock(bool x=true,int p=-1); /** * Unlock a previously locked mutex, and restart any threads that * are waiting on this mutex. * * @return 0 on success, EBUSY if it is locked by another process, and EINVAL if the mutex is an unknown kind. */ int unLock(); /** * Tell us wether the mutex is locked or not. * * @return true if locked. */ int isLocked(); /** * The following methods allow the specification of different mutex * variable kinds. The differences are in how they are treated, not * in how they function. See the next method, for a discussion on * mutex kinds. * * @return fast for a fast mutex, and recursive for a mutex that can be locked several times by it's owner. */ int kind(); /** * This method, sets the mutex kind and returns the old value. The * valid mutex kinds are: * * <pre> * mutex_kind::fast_e - can only be locked once, by any process. * mutex_kind::recursive_e - can be locked multiple times, by the owner. * of the original lock. it must be unlocked * equally many times. * mutex_kind::errorcheck_e - this is a fast mutex, but with simple checks * that will enable the user to detect possible * dedlocks. * </pre> * * @param kind The mutex kind, see above. * @return the previous kind, prior to setting. */ int kind(mutex_kind); /** * The shared memory scheme, wants a single name to identify the * overall program. It is possible, and perhaps desired that * part cond/mutex/semaphore have their own name identification * tree. This is possible, by stating that it should be branch * of the main file name, with a give extension. * * <pre> * main() * { * Mutex *ml; * * Pthread::setProject( "my_project" ); * Mutex::projectPart( "Mutex" ); * ml = new Mutex(attributes::process_shared_e); * ... * } * </pre> * * This will set the project to my_project, and all mutexes * will be derived from my_project_mutex. * * @param part A C string containing the mutex part name. */ static void projectPart(const std::string&); /** * Query wether the mutex is locked, or not. * * @return true if the mutex is locked. */ operator bool(); };}; // namespace#endif /* THREADS MUTEX */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -