File: Sema.h

package info (click to toggle)
storm-lang 0.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,100 kB
  • sloc: ansic: 261,471; cpp: 140,438; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (53 lines) | stat: -rw-r--r-- 1,260 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include "Core/Object.h"

namespace storm {
	STORM_PKG(core.sync);

	/**
	 * A basic semaphore usable from whitin Storm. If possible, use a plain os::Lock or os::Sema as
	 * it does not require any separate allocations.
	 *
	 * This object slightly breaks the expected semantics of Storm as it does not make sense to copy
	 * a semaphore. Instead, all copies will refer to the same semaphore.
	 */
	class Sema : public Object {
		STORM_CLASS;
	public:
		// Create a semaphore initialized to 1.
		STORM_CTOR Sema();

		// Create a semaphore initialized to `count`.
		STORM_CTOR Sema(Nat count);

		// Copy.
		Sema(const Sema &o);

		// Destroy.
		~Sema();

		// Deep copy.
		void STORM_FN deepCopy(CloneEnv *e);

		// Increase the value in the semaphore by 1. May wake any waiting threads.
		void STORM_FN up();

		// Attempt to decrease the value in the semaphore by 1. Will wait in case the value would
		// become negative.
		void STORM_FN down();

	private:
		// Separate allocation for the actual lock. Allocated outside of the Gc since we do not
		// allow barriers or movement of the memory allocated for the lock.
		struct Data {
			size_t refs;
			os::Sema sema;

			Data(Nat count);
		};

		// Allocation. Not GC:d.
		Data *alloc;
	};

}