Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
Condition Struct Reference

#include <Condition.h>

Overview

POSIX Threads conditional variables.

Conditions combine a Lock with a signaling mechanism, so that Threads may inform one another when a condition is met.

Definition at line 44 of file Condition.h.

Inheritance diagram for Condition:
Lock Object

Properties

Lock lock
 The superclass. More...
 
- Properties inherited from Lock
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Condition (void)
 The Condition archetype. More...
 
void broadcast (Condition *self)
 Signals all Threads waiting on this Condition. More...
 
Conditioninit (Condition *self)
 Initializes this Condition. More...
 
void signal (Condition *self)
 Signals a single Thread waiting on this Condition. More...
 
void wait (Condition *self)
 Waits indefinitely for this Condition to be signaled. More...
 
_Bool waitUntilDate (Condition *self, const Date *date)
 Waits until the specified Date for this Condition to be signaled. More...
 
- Methods inherited from Lock
Class_Lock (void)
 The Lock archetype. More...
 
Lockinit (Lock *self)
 Initializes this Lock. More...
 
void lock (Lock *self)
 Acquire this lock, waiting indefinitely. More...
 
_Bool tryLock (Lock *self)
 Attempt to acquire this lock immediately. More...
 
void unlock (Lock *self)
 Release this Lock. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

ConditionInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Lock
LockInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ interface

ConditionInterface* Condition::interface
protected

The interface.

Definition at line 55 of file Condition.h.

◆ lock

Lock Condition::lock

The superclass.

Definition at line 49 of file Condition.h.

Method Details

◆ _Condition()

Class * _Condition ( void  )

The Condition archetype.

Returns
The Condition Class.

Definition at line 141 of file Condition.c.

141 {
142 static Class *clazz;
143 static Once once;
144
145 do_once(&once, {
146 clazz = _initialize(&(const ClassDef) {
147 .name = "Condition",
148 .superclass = _Lock(),
149 .instanceSize = sizeof(Condition),
150 .interfaceOffset = offsetof(Condition, interface),
151 .interfaceSize = sizeof(ConditionInterface),
153 });
154 });
155
156 return clazz;
157}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Condition.c:126
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
POSIX Threads conditional variables.
Definition: Condition.h:44
ConditionInterface * interface
The interface.
Definition: Condition.h:55
Class * _Lock(void)
The Lock archetype.
Definition: Lock.c:129
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51

◆ broadcast()

void broadcast ( Condition self)

Signals all Threads waiting on this Condition.

Parameters
selfThe Condition.
Remarks
This method should only be called when the Condition is locked.

Definition at line 57 of file Condition.c.

57 {
58
59 int err = pthread_cond_broadcast(self->condition);
60 assert(err == 0);
61}

◆ init()

Condition * init ( Condition self)

Initializes this Condition.

Parameters
selfThe Condition.
Returns
The initialized Condition, or NULL on error.

Definition at line 67 of file Condition.c.

67 {
68
69 self = (Condition *) super(Lock, self, init);
70 if (self) {
71
72 self->condition = calloc(1, sizeof(pthread_cond_t));
73 assert(self->condition);
74
75 const int err = pthread_cond_init(self->condition, NULL);
76 assert(err == 0);
77 }
78
79 return self;
80}
#define super(type, obj, method,...)
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
POSIX Threads locks.
Definition: Lock.h:42

◆ signal()

void signal ( Condition self)

Signals a single Thread waiting on this Condition.

Parameters
selfThe Condition.
Remarks
This method should only be called when the Condition is locked.

◆ wait()

void wait ( Condition self)

Waits indefinitely for this Condition to be signaled.

Parameters
selfThe Condition.
Remarks
This method should only be called when the Condition is locked.

◆ waitUntilDate()

_Bool waitUntilDate ( Condition self,
const Date date 
)

Waits until the specified Date for this Condition to be signaled.

Parameters
selfThe Condition.
dateThe Date until which to wait.
Returns
true if this Condition was signaled before date, false otherwise.
Remarks
This method should only be called when the Condition is locked.

Definition at line 106 of file Condition.c.

106 {
107
108 Lock *lock = (Lock *) self;
109
110 const struct timespec time = {
111 .tv_sec = date->time.tv_sec,
112 .tv_nsec = date->time.tv_usec * 1000
113 };
114
115 int err = pthread_cond_timedwait(self->condition, lock->lock, &time);
116 assert(err == 0 || err == ETIMEDOUT);
117
118 return err == 0;
119}
static Date * date(void)
Definition: Date.c:98
Lock lock
The superclass.
Definition: Condition.h:49
Time time
The time.
Definition: Date.h:86

The documentation for this struct was generated from the following files: