1c041511dScube/***********************************************************
2c041511dScube *	Copyright (C) 1997, Be Inc.  Copyright (C) 1999, Jake Hamby.
3c041511dScube *
4c041511dScube * This program is freely distributable without licensing fees
5c041511dScube * and is provided without guarantee or warrantee expressed or
6c041511dScube * implied. This program is -not- in the public domain.
7c041511dScube *
8c041511dScube *  FILE:	glutBlocker.cpp
9c041511dScube *
10c041511dScube *	DESCRIPTION:	helper class for GLUT event loop.
11c041511dScube *		if a window receives an event, wake up the event loop.
12c041511dScube ***********************************************************/
13c041511dScube
14c041511dScube/***********************************************************
15c041511dScube *	Headers
16c041511dScube ***********************************************************/
17c041511dScube#include "glutBlocker.h"
18c041511dScube
19c041511dScube/***********************************************************
20c041511dScube *	Global variable
21c041511dScube ***********************************************************/
22c041511dScubeGlutBlocker gBlock;
23c041511dScube
24c041511dScube/***********************************************************
25c041511dScube *	Member functions
26c041511dScube ***********************************************************/
27c041511dScubeGlutBlocker::GlutBlocker() {
28c041511dScube	gSem = create_sem(1, "gSem");
29c041511dScube	eSem = create_sem(0, "eSem");
30c041511dScube	events = false;
31c041511dScube	sleeping = false;
32c041511dScube}
33c041511dScube
34c041511dScubeGlutBlocker::~GlutBlocker() {
35c041511dScube	delete_sem(eSem);
36c041511dScube	delete_sem(gSem);
37c041511dScube}
38c041511dScube
39c041511dScubevoid GlutBlocker::WaitEvent() {
40c041511dScube	acquire_sem(gSem);
41c041511dScube	if(!events) {			// wait for new event
42c041511dScube		sleeping = true;
43c041511dScube		release_sem(gSem);
44c041511dScube		acquire_sem(eSem);	// next event will release eSem
45c041511dScube	} else {
46c041511dScube		release_sem(gSem);
47c041511dScube	}
48c041511dScube}
49c041511dScube
50c041511dScubevoid GlutBlocker::WaitEvent(bigtime_t usecs) {
51c041511dScube	acquire_sem(gSem);
52c041511dScube	if(!events) {			// wait for new event
53c041511dScube		sleeping = true;
54c041511dScube		release_sem(gSem);
55c041511dScube		acquire_sem_etc(eSem, 1, B_TIMEOUT, usecs);	// wait for next event or timeout
56c041511dScube	} else {
57c041511dScube		release_sem(gSem);
58c041511dScube	}
59c041511dScube}
60c041511dScube
61c041511dScubevoid GlutBlocker::NewEvent() {
62c041511dScube	acquire_sem(gSem);
63c041511dScube	events = true;		// next call to WaitEvent returns immediately
64c041511dScube	if(sleeping) {
65c041511dScube		sleeping = false;
66c041511dScube		release_sem(eSem);	// if event loop is blocking, wake it up
67c041511dScube	}
68c041511dScube	release_sem(gSem);
69c041511dScube}
70