Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
Lock.c File Reference
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include "Lock.h"

Go to the source code of this file.

Macros

#define _Class   _Lock
 

Functions

Class_Lock (void)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Lockinit (Lock *self)
 
static void initialize (Class *clazz)
 
static void lock (Lock *self)
 
static _Bool tryLock (Lock *self)
 
static void unlock (Lock *self)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Lock

Definition at line 32 of file Lock.c.

Function Documentation

◆ _Lock()

Class * _Lock ( void  )

Definition at line 129 of file Lock.c.

129 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Lock.c:114
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 locks.
Definition: Lock.h:42
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 39 of file Lock.c.

39 {
40 return NULL;
41}

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 46 of file Lock.c.

46 {
47
48 Lock *this = (Lock *) self;
49
50 pthread_mutex_destroy(this->lock);
51 free(this->lock);
52
53 super(Object, self, dealloc);
54}
#define super(type, obj, method,...)
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

◆ init()

static Lock * init ( Lock self)
static

Definition at line 62 of file Lock.c.

62 {
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}
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 114 of file Lock.c.

114 {
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}
ident interface
The interface of the Class.
Definition: Class.h:105
Lock lock
The superclass.
Definition: Condition.h:49
_Bool tryLock(Lock *self)
Attempt to acquire this lock immediately.
Definition: Lock.c:91
void unlock(Lock *self)
Release this Lock.
Definition: Lock.c:103
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ lock()

static void lock ( Lock self)
static

Definition at line 81 of file Lock.c.

81 {
82
83 int err = pthread_mutex_lock(self->lock);
84 assert(err == 0);
85}

◆ tryLock()

static _Bool tryLock ( Lock self)
static

Definition at line 91 of file Lock.c.

91 {
92
93 int err = pthread_mutex_trylock(self->lock);
94 assert(err == 0 || err == EBUSY);
95
96 return err == 0;
97}

◆ unlock()

static void unlock ( Lock self)
static

Definition at line 103 of file Lock.c.

103 {
104
105 int err = pthread_mutex_unlock(self->lock);
106 assert(err == 0);
107}