File:  [parser3project] / parser3 / src / include / pa_threads.h
Revision 1.30: download - view: text, annotated - select for diffs - revision graph
Tue Aug 9 08:14:50 2005 UTC (20 years, 11 months ago) by paf
Branches: MAIN
CVS tags: release_3_4_1, release_3_4_0, release_3_3_0, release_3_2_3, release_3_2_2, release_3_2_1, release_3_2_0, paf_left, HEAD
2005

/** @file
	Parser: mutex & helpers decls.

	Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
	Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
*/

#ifndef PA_THREADS_H
#define PA_THREADS_H

static const char * const IDENT_THREADS_H="$Date: 2005/08/09 08:14:50 $";

#include "pa_config_includes.h"
#include "pa_types.h"

/// thread ID type
typedef unsigned int pa_thread_t;

/// get caller thread ID
pa_thread_t pa_get_thread_id();

class AutoSYNCHRONIZED;

/// simple semaphore object
class Mutex {
	friend class AutoSYNCHRONIZED;
private:
	uint handle;
public:
	Mutex();
	~Mutex();
private: // for AutoSYNCHRONIZED
	void acquire();
	void release();
};

extern const bool parser_multithreaded;
extern Mutex global_mutex;

/** 
	Helper to ensure paired Mutex::acquire() and Mutex::release().

	Use it with SYNCHRONIZED macro
*/
class AutoSYNCHRONIZED {
public:
	AutoSYNCHRONIZED() { global_mutex.acquire(); }
	~AutoSYNCHRONIZED() { global_mutex.release(); }
};

/** 
	put it to first line of a function to ensure thread safety.
	@verbatim
		void someclass::somefunc(...) { SYNCHRONIZED;
			...
		}
	@endverbatim

	WARNING: don't use THROW or PTHROW with such thread safety mechanizm -
	longjmp would leave global_mutex acquired, which is wrong!
*/
#define SYNCHRONIZED AutoSYNCHRONIZED autoSYNCHRONIZED

#endif

E-mail: