filelock

class for locking files in filesystem
git clone https://noulin.net/git/filelock.git
Log | Files | Refs | LICENSE

filelock.h (3201B)


      1 #pragma once
      2 
      3 #define acquireO(obj) (obj)->f->acquire(obj)
      4 #define acquireG acquireO
      5 
      6 #define releaseO(obj) (obj)->f->release(obj)
      7 #define releaseG releaseO
      8 
      9 #define setFileO(obj, path) (obj)->f->set(obj, path)
     10 #define setFileG setFileO
     11 
     12 #define getFileO(obj) (obj)->f->get(obj)
     13 #define getFileG getFileO
     14 
     15 // Class filelock
     16 typedef struct filelock filelockt;
     17 
     18 // for object inheriting filelock, cast to filelock to be able to use this class functions and generics
     19 #define cFilelock(self) ( (filelockt*) self )
     20 
     21 typedef void            (*freeFilelockFt)      (filelockt *self);
     22 typedef void            (*terminateFilelockFt) (filelockt **self);
     23 typedef char*           (*toStringFilelockFt)  (filelockt *self);
     24 typedef filelockt* (*duplicateFilelockFt) (filelockt *self);
     25 typedef void            (*smashFilelockFt)     (filelockt **self);
     26 
     27 /**
     28  * free filelock
     29  */
     30 typedef void            (*finishFilelockFt)    (filelockt **self);
     31 
     32 typedef void            (*setDelaysFilelockFt) (filelockt *self, f32 timeout, f32 delay);
     33 typedef void            (*logFilelockFt)       (filelockt *self);
     34 typedef bool            (*acquireFilelockFt)   (filelockt *self);
     35 typedef void            (*releaseFilelockFt)   (filelockt *self);
     36 typedef void            (*setFilelockFt)       (filelockt *self, const char *filepath);
     37 typedef const char*     (*getFilelockFt)       (filelockt *self);
     38 
     39 /**
     40  * class functions
     41  * allocated once for all objects
     42  *
     43  * freed with finalizeFilelock or finalizeLibsheepy
     44  */
     45 
     46 /**
     47  * use this define in child classes and add the new function after this class functions
     48  *
     49  * in this define, add the methods after <finishFilelockFt    finish;>
     50  *
     51  * Example:
     52  * #define RINGFUNCTIONST \
     53  *   CLASSTEMPLATEFUNCTIONST; \
     54  *   setSizeRingFt           setSize
     55  */
     56 #define CLASSTEMPLATEFUNCTIONST \
     57   setDelaysFilelockFt setDelays;\
     58   logFilelockFt       log;\
     59   acquireFilelockFt   acquire;\
     60   releaseFilelockFt   release;\
     61   setFilelockFt       set;\
     62   getFilelockFt       get
     63 
     64 typedef struct {
     65   freeFilelockFt      free;
     66   terminateFilelockFt terminate;
     67   toStringFilelockFt  toString;
     68   duplicateFilelockFt duplicate;
     69   smashFilelockFt     smash;
     70   finishFilelockFt    finish;
     71   CLASSTEMPLATEFUNCTIONST;
     72 } filelockFunctionst;
     73 
     74 /**
     75  * class
     76  */
     77 struct filelock {
     78   const char         *type;
     79   filelockFunctionst *f;
     80 
     81   // private
     82   bool               isLocked;
     83   char               *filename;
     84   char               *lockfile;
     85   f32                timeout;
     86   f32                delay;
     87   int                fd;
     88 };
     89 
     90 // filelock
     91 
     92 #define createFilelock(obj) filelockt obj; initiateFilelock(&obj)
     93 #define createAllocateFilelock(obj) filelockt *obj; initiateAllocateFilelock(&obj)
     94 
     95 void initiateFilelock(filelockt *self);
     96 void initiateAllocateFilelock(filelockt **self);
     97 void finalizeFilelock(void);
     98 
     99 // initialize class methods, call registerMethodsFilelock from classes inheriting this class
    100 void registerMethodsFilelock(filelockFunctionst *f);
    101 
    102 filelockt* allocFilelock(const char *filepath);
    103 
    104 // end class filelock
    105 
    106 #define isFilelockCompiledWithCurrentLisheepyVersion checkLibsheepyVersionFilelock(LIBSHEEPY_VERSION)
    107 bool checkLibsheepyVersionFilelock(const char *currentLibsheepyVersion);