github.com/256dpi/max-go@v0.7.0/lib/max/ext_critical.h (about) 1 // critical.h -- platform-independent way to define critical regions 2 3 #ifndef _EXT_CRITICAL_H_ 4 #define _EXT_CRITICAL_H_ 5 6 #include "ext_prefix.h" 7 8 BEGIN_USING_C_LINKAGE 9 10 #if C74_PRAGMA_STRUCT_PACKPUSH 11 #pragma pack(push, 2) 12 #elif C74_PRAGMA_STRUCT_PACK 13 #pragma pack(2) 14 #endif 15 16 #ifdef WIN_VERSION 17 18 typedef LPCRITICAL_SECTION t_critical; ///< a critical region @ingroup critical 19 20 #endif // WIN_VERSION 21 22 23 #if defined(MAC_VERSION) || defined(LINUX_VERSION) 24 25 #include <pthread.h> 26 27 typedef pthread_mutex_t* t_critical; ///< a critical region @ingroup critical 28 29 #endif // WIN_VERSION 30 31 32 33 34 35 /** 36 Create a new critical region. 37 Normally, you do not need to create your own critical region, because 38 you can use Max’s global critical region. Only use this function (in 39 your object’s instance creation method) if you are certain you are not 40 able to use the global critical region. 41 42 @ingroup critical 43 @param x A #t_critical struct will be returned to you via this pointer. 44 */ 45 void critical_new(t_critical *x); 46 47 48 /** 49 Enter a critical region. 50 Typically you will want the argument to be zero to enter the global 51 critical region, although you could pass your own critical created with 52 critical_new(). It is important to try to keep the amount of code in 53 the critical region to a minimum. 54 Exit the critical region with critical_exit(). 55 56 @ingroup critical 57 @param x A pointer to a #t_critical struct, or zero to uses Max’s global critical region. 58 @see critical_exit() 59 */ 60 void critical_enter(t_critical x); 61 62 63 /** 64 Leave a critical region. 65 Typically you will want the argument to be zero to exit the global 66 critical region, although, you if you are using your own critical regions 67 you will want to pass the same one that you previously passed to 68 critical_enter(). 69 70 @ingroup critical 71 @param x A pointer to a #t_critical struct, or zero to uses Max’s global critical region. 72 */ 73 void critical_exit(t_critical x); 74 75 76 /** 77 Free a critical region created with critical_new(). 78 If you created your own critical region, you will need to free it in your object’s free method. 79 80 @ingroup critical 81 @param x The #t_critical struct that will be freed. 82 */ 83 void critical_free(t_critical x); 84 85 86 /** Try to enter a critical region if it is not locked. 87 @ingroup critical 88 @param x A pointer to a #t_critical struct, or zero to uses Max’s global critical region. 89 @return returns non-zero if there was a problem entering 90 @see critical_enter() 91 */ 92 short critical_tryenter(t_critical x); 93 94 95 #if C74_PRAGMA_STRUCT_PACKPUSH 96 #pragma pack(pop) 97 #elif C74_PRAGMA_STRUCT_PACK 98 #pragma pack() 99 #endif 100 101 END_USING_C_LINKAGE 102 103 #endif // _EXT_CRITICAL_H_