Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Lock.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#include <errno.h>
26#include <stdlib.h>
27
28#include <pthread.h>
29
30#include "Lock.h"
31
32#define _Class _Lock
33
34#pragma mark - Object
35
39static Object *copy(const Object *self) {
40 return NULL;
41}
42
46static void dealloc(Object *self) {
47
48 Lock *this = (Lock *) self;
49
50 pthread_mutex_destroy(this->lock);
51 free(this->lock);
52
53 super(Object, self, dealloc);
54}
55
56#pragma mark - Lock
57
62static Lock *init(Lock *self) {
63
64 self = (Lock *) super(Object, self, init);
65 if (self) {
66
67 self->lock = calloc(1, sizeof(pthread_mutex_t));
68 assert(self->lock);
69
70 const int err = pthread_mutex_init(self->lock, NULL);
71 assert(err == 0);
72 }
73
74 return self;
75}
76
81static void lock(Lock *self) {
82
83 int err = pthread_mutex_lock(self->lock);
84 assert(err == 0);
85}
86
91static _Bool tryLock(Lock *self) {
92
93 int err = pthread_mutex_trylock(self->lock);
94 assert(err == 0 || err == EBUSY);
95
96 return err == 0;
97}
98
103static void unlock(Lock *self) {
104
105 int err = pthread_mutex_unlock(self->lock);
106 assert(err == 0);
107}
108
109#pragma mark - Class lifecycle
110
114static void initialize(Class *clazz) {
115
116 ((ObjectInterface *) clazz->interface)->copy = copy;
117 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
118
119 ((LockInterface *) clazz->interface)->init = init;
120 ((LockInterface *) clazz->interface)->lock = lock;
121 ((LockInterface *) clazz->interface)->tryLock = tryLock;
122 ((LockInterface *) clazz->interface)->unlock = unlock;
123}
124
129Class *_Lock(void) {
130 static Class *clazz;
131 static Once once;
132
133 do_once(&once, {
134 clazz = _initialize(&(const ClassDef) {
135 .name = "Lock",
136 .superclass = _Object(),
137 .instanceSize = sizeof(Lock),
138 .interfaceOffset = offsetof(Lock, interface),
139 .interfaceSize = sizeof(LockInterface),
141 });
142 });
143
144 return clazz;
145}
146
147#undef _Class
148
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define super(type, obj, method,...)
static void initialize(Class *clazz)
Definition: Lock.c:114
POSIX Threads locks.
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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Lock lock
The superclass.
Definition: Condition.h:49
POSIX Threads locks.
Definition: Lock.h:42
Class * _Lock(void)
The Lock archetype.
Definition: Lock.c:129
_Bool tryLock(Lock *self)
Attempt to acquire this lock immediately.
Definition: Lock.c:91
void lock(Lock *self)
Acquire this lock, waiting indefinitely.
Definition: Lock.c:81
void unlock(Lock *self)
Release this Lock.
Definition: Lock.c:103
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50