Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Condition.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include <errno.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <pthread.h>
31
32#include "Condition.h"
33
34#define _Class _Condition
35
36#pragma mark - Object
37
41static void dealloc(Object *self) {
42
43 Condition *this = (Condition *) self;
44
45 pthread_cond_destroy(this->condition);
46 free(this->condition);
47
48 super(Object, self, dealloc);
49}
50
51#pragma mark - Condition
52
57static void broadcast(Condition *self) {
58
59 int err = pthread_cond_broadcast(self->condition);
60 assert(err == 0);
61}
62
67static Condition *init(Condition *self) {
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}
81
86static void _signal(Condition *self) {
87
88 int err = pthread_cond_signal(self->condition);
89 assert(err == 0);
90}
91
96static void _wait(Condition *self) {
97
98 int err = pthread_cond_wait(self->condition, self->lock.lock);
99 assert(err == 0);
100}
101
106static _Bool waitUntilDate(Condition *self, const Date *date) {
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}
120
121#pragma mark - Class lifecycle
122
126static void initialize(Class *clazz) {
127
128 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
129
130 ((ConditionInterface *) clazz->interface)->broadcast = broadcast;
131 ((ConditionInterface *) clazz->interface)->init = init;
132 ((ConditionInterface *) clazz->interface)->signal = _signal;
133 ((ConditionInterface *) clazz->interface)->wait = _wait;
134 ((ConditionInterface *) clazz->interface)->waitUntilDate = waitUntilDate;
135}
136
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}
158
159#undef _Class
160
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define super(type, obj, method,...)
static void _signal(Condition *self)
Definition: Condition.c:86
static void _wait(Condition *self)
Definition: Condition.c:96
static void initialize(Class *clazz)
Definition: Condition.c:126
POSIX Threads conditional variables.
Immutable UTF-8 strings.
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
ident interface
The interface of the Class.
Definition: Class.h:105
POSIX Threads conditional variables.
Definition: Condition.h:44
_Bool waitUntilDate(Condition *self, const Date *date)
Waits until the specified Date for this Condition to be signaled.
Definition: Condition.c:106
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Lock lock
The superclass.
Definition: Condition.h:49
void signal(Condition *self)
Signals a single Thread waiting on this Condition.
Class * _Condition(void)
The Condition archetype.
Definition: Condition.c:141
void broadcast(Condition *self)
Signals all Threads waiting on this Condition.
Definition: Condition.c:57
Microsecond-precision immutable dates.
Definition: Date.h:70
Time time
The time.
Definition: Date.h:86
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98
POSIX Threads locks.
Definition: Lock.h:42
Class * _Lock(void)
The Lock archetype.
Definition: Lock.c:129
void lock(Lock *self)
Acquire this lock, waiting indefinitely.
Definition: Lock.c:81
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50