Use atomic functions where appropriate

Also, rename atomic functions to be consistent with the rest of the
platform/threading functions, and move atomic functions to threading*
files rather than platform* files
This commit is contained in:
jp9000 2014-03-16 18:26:46 -07:00
parent 3ed647b8a0
commit 154e0c59e1
15 changed files with 84 additions and 76 deletions

View File

@ -18,6 +18,7 @@
#pragma once #pragma once
#include <util/darray.h> #include <util/darray.h>
#include <util/threading.h>
#include <graphics/graphics.h> #include <graphics/graphics.h>
#include <graphics/matrix4.h> #include <graphics/matrix4.h>
@ -285,12 +286,12 @@ struct gs_sampler_state {
static inline void samplerstate_addref(samplerstate_t ss) static inline void samplerstate_addref(samplerstate_t ss)
{ {
ss->ref++; os_atomic_inc_long(&ss->ref);
} }
static inline void samplerstate_release(samplerstate_t ss) static inline void samplerstate_release(samplerstate_t ss)
{ {
if (--ss->ref == 0) if (os_atomic_dec_long(&ss->ref) == 0)
bfree(ss); bfree(ss);
} }

View File

@ -241,5 +241,5 @@ struct graphics_subsystem {
DARRAY(struct vec2) texverts[16]; DARRAY(struct vec2) texverts[16];
pthread_mutex_t mutex; pthread_mutex_t mutex;
volatile int ref; volatile long ref;
}; };

View File

@ -179,13 +179,13 @@ void gs_entercontext(graphics_t graphics)
thread_graphics = graphics; thread_graphics = graphics;
} }
graphics->ref++; os_atomic_inc_long(&graphics->ref);
} }
void gs_leavecontext(void) void gs_leavecontext(void)
{ {
if (thread_graphics) { if (thread_graphics) {
if (!--thread_graphics->ref) { if (!os_atomic_dec_long(&thread_graphics->ref)) {
graphics_t graphics = thread_graphics; graphics_t graphics = thread_graphics;
graphics->exports.device_leavecontext(graphics->device); graphics->exports.device_leavecontext(graphics->device);

View File

@ -16,11 +16,12 @@
******************************************************************************/ ******************************************************************************/
#include "util/bmem.h" #include "util/bmem.h"
#include "util/threading.h"
#include "util/darray.h" #include "util/darray.h"
#include "obs-data.h" #include "obs-data.h"
struct obs_data_item { struct obs_data_item {
volatile int ref; volatile long ref;
struct obs_data *parent; struct obs_data *parent;
struct obs_data_item *next; struct obs_data_item *next;
enum obs_data_type type; enum obs_data_type type;
@ -30,13 +31,13 @@ struct obs_data_item {
}; };
struct obs_data { struct obs_data {
volatile int ref; volatile long ref;
char *json; char *json;
struct obs_data_item *first_item; struct obs_data_item *first_item;
}; };
struct obs_data_array { struct obs_data_array {
volatile int ref; volatile long ref;
DARRAY(obs_data_t) objects; DARRAY(obs_data_t) objects;
}; };
@ -244,7 +245,7 @@ obs_data_t obs_data_create_from_json(const char *json_string)
void obs_data_addref(obs_data_t data) void obs_data_addref(obs_data_t data)
{ {
if (data) if (data)
++data->ref; os_atomic_inc_long(&data->ref);
} }
static inline void obs_data_destroy(struct obs_data *data) static inline void obs_data_destroy(struct obs_data *data)
@ -265,7 +266,7 @@ void obs_data_release(obs_data_t data)
{ {
if (!data) return; if (!data) return;
if (--data->ref == 0) if (os_atomic_dec_long(&data->ref) == 0)
obs_data_destroy(data); obs_data_destroy(data);
} }
@ -466,7 +467,7 @@ obs_data_t obs_data_getobj(obs_data_t data, const char *name)
obs_data_t obj = get_item_obj(item); obs_data_t obj = get_item_obj(item);
if (obj) if (obj)
obj->ref++; os_atomic_inc_long(&obj->ref);
return obj; return obj;
} }
@ -476,7 +477,7 @@ obs_data_array_t obs_data_getarray(obs_data_t data, const char *name)
obs_data_array_t array = get_item_array(item); obs_data_array_t array = get_item_array(item);
if (array) if (array)
array->ref++; os_atomic_inc_long(&array->ref);
return array; return array;
} }
@ -491,7 +492,7 @@ obs_data_array_t obs_data_array_create()
void obs_data_array_addref(obs_data_array_t array) void obs_data_array_addref(obs_data_array_t array)
{ {
if (array) if (array)
++array->ref; os_atomic_inc_long(&array->ref);
} }
static inline void obs_data_array_destroy(obs_data_array_t array) static inline void obs_data_array_destroy(obs_data_array_t array)
@ -509,7 +510,7 @@ void obs_data_array_release(obs_data_array_t array)
if (!array) if (!array)
return; return;
if (--array->ref == 0) if (os_atomic_dec_long(&array->ref) == 0)
obs_data_array_destroy(array); obs_data_array_destroy(array);
} }
@ -528,7 +529,7 @@ obs_data_t obs_data_array_item(obs_data_array_t array, size_t idx)
data = (idx < array->objects.num) ? array->objects.array[idx] : NULL; data = (idx < array->objects.num) ? array->objects.array[idx] : NULL;
if (data) if (data)
data->ref++; os_atomic_inc_long(&data->ref);
return data; return data;
} }
@ -537,7 +538,7 @@ size_t obs_data_array_push_back(obs_data_array_t array, obs_data_t obj)
if (!array || !obj) if (!array || !obj)
return 0; return 0;
obj->ref++; os_atomic_inc_long(&obj->ref);
return da_push_back(array->objects, &obj); return da_push_back(array->objects, &obj);
} }
@ -546,7 +547,7 @@ void obs_data_array_insert(obs_data_array_t array, size_t idx, obs_data_t obj)
if (!array || !obj) if (!array || !obj)
return; return;
obj->ref++; os_atomic_inc_long(&obj->ref);
da_insert(array->objects, idx, &obj); da_insert(array->objects, idx, &obj);
} }
@ -567,7 +568,7 @@ obs_data_item_t obs_data_first(obs_data_t data)
return NULL; return NULL;
if (data->first_item) if (data->first_item)
data->first_item->ref++; os_atomic_inc_long(&data->first_item->ref);
return data->first_item; return data->first_item;
} }
@ -578,18 +579,20 @@ obs_data_item_t obs_data_item_byname(obs_data_t data, const char *name)
struct obs_data_item *item = get_item(data, name); struct obs_data_item *item = get_item(data, name);
if (item) if (item)
item->ref++; os_atomic_inc_long(&item->ref);
return item; return item;
} }
bool obs_data_item_next(obs_data_item_t *item) bool obs_data_item_next(obs_data_item_t *item)
{ {
if (item && *item) { if (item && *item) {
(*item)->ref--; obs_data_item_t next = (*item)->next;
*item = (*item)->next; obs_data_item_release(item);
if (*item) { *item = next;
(*item)->ref++;
if (next) {
os_atomic_inc_long(&next->ref);
return true; return true;
} }
} }
@ -600,7 +603,7 @@ bool obs_data_item_next(obs_data_item_t *item)
void obs_data_item_release(obs_data_item_t *item) void obs_data_item_release(obs_data_item_t *item)
{ {
if (item && *item) { if (item && *item) {
int ref = --(*item)->ref; long ref = os_atomic_dec_long(&(*item)->ref);
if (!ref) { if (!ref) {
obs_data_item_destroy(*item); obs_data_item_destroy(*item);
*item = NULL; *item = NULL;
@ -689,7 +692,7 @@ obs_data_t obs_data_item_getobj(obs_data_item_t item)
get_item_obj(item) : NULL; get_item_obj(item) : NULL;
if (obj) if (obj)
obj->ref++; os_atomic_inc_long(&obj->ref);
return obj; return obj;
} }
@ -699,6 +702,6 @@ obs_data_array_t obs_data_item_getarray(obs_data_item_t item)
get_item_array(item) : NULL; get_item_array(item) : NULL;
if (array) if (array)
array->ref++; os_atomic_inc_long(&array->ref);
return array; return array;
} }

View File

@ -176,7 +176,7 @@ extern void *obs_video_thread(void *param);
/* sources */ /* sources */
struct obs_source { struct obs_source {
volatile int refs; volatile long refs;
struct obs_source_info info; struct obs_source_info info;
/* source-specific data */ /* source-specific data */
@ -188,13 +188,13 @@ struct obs_source {
proc_handler_t procs; proc_handler_t procs;
/* ensures show/hide are only called once */ /* ensures show/hide are only called once */
int show_refs; volatile long show_refs;
/* ensures activate/deactivate are only called once */ /* ensures activate/deactivate are only called once */
int activate_refs; volatile long activate_refs;
/* prevents infinite recursion when enumerating sources */ /* prevents infinite recursion when enumerating sources */
int enum_refs; volatile long enum_refs;
/* used to indicate that the source has been removed and all /* used to indicate that the source has been removed and all
* references to it should be released (not exactly how I would prefer * references to it should be released (not exactly how I would prefer
@ -204,7 +204,7 @@ struct obs_source {
/* timing (if video is present, is based upon video) */ /* timing (if video is present, is based upon video) */
volatile bool timing_set; volatile bool timing_set;
volatile uint64_t timing_adjust; volatile uint64_t timing_adjust;
volatile int audio_reset_ref; volatile long audio_reset_ref;
uint64_t next_audio_ts_min; uint64_t next_audio_ts_min;
uint64_t last_frame_ts; uint64_t last_frame_ts;
uint64_t last_sys_timestamp; uint64_t last_sys_timestamp;

View File

@ -15,6 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/ ******************************************************************************/
#include "util/threading.h"
#include "graphics/math-defs.h" #include "graphics/math-defs.h"
#include "obs-scene.h" #include "obs-scene.h"
@ -373,7 +374,7 @@ static void obs_sceneitem_destroy(obs_sceneitem_t item)
void obs_sceneitem_addref(obs_sceneitem_t item) void obs_sceneitem_addref(obs_sceneitem_t item)
{ {
if (item) if (item)
++item->ref; os_atomic_inc_long(&item->ref);
} }
void obs_sceneitem_release(obs_sceneitem_t item) void obs_sceneitem_release(obs_sceneitem_t item)
@ -381,7 +382,7 @@ void obs_sceneitem_release(obs_sceneitem_t item)
if (!item) if (!item)
return; return;
if (--item->ref == 0) if (os_atomic_dec_long(&item->ref) == 0)
obs_sceneitem_destroy(item); obs_sceneitem_destroy(item);
} }

View File

@ -23,7 +23,7 @@
/* how obs scene! */ /* how obs scene! */
struct obs_scene_item { struct obs_scene_item {
volatile int ref; volatile long ref;
volatile bool removed; volatile bool removed;
struct obs_scene *parent; struct obs_scene *parent;

View File

@ -19,6 +19,7 @@
#include "media-io/format-conversion.h" #include "media-io/format-conversion.h"
#include "media-io/video-frame.h" #include "media-io/video-frame.h"
#include "util/threading.h"
#include "util/platform.h" #include "util/platform.h"
#include "callback/calldata.h" #include "callback/calldata.h"
#include "graphics/matrix3.h" #include "graphics/matrix3.h"
@ -268,7 +269,7 @@ static void obs_source_destroy(struct obs_source *source)
void obs_source_addref(obs_source_t source) void obs_source_addref(obs_source_t source)
{ {
if (source) if (source)
++source->refs; os_atomic_inc_long(&source->refs);
} }
void obs_source_release(obs_source_t source) void obs_source_release(obs_source_t source)
@ -276,7 +277,7 @@ void obs_source_release(obs_source_t source)
if (!source) if (!source)
return; return;
if (--source->refs == 0) if (os_atomic_dec_long(&source->refs) == 0)
obs_source_destroy(source); obs_source_destroy(source);
} }
@ -380,7 +381,7 @@ static void hide_source(obs_source_t source)
static void activate_tree(obs_source_t parent, obs_source_t child, void *param) static void activate_tree(obs_source_t parent, obs_source_t child, void *param)
{ {
if (++child->activate_refs == 1) if (os_atomic_inc_long(&child->activate_refs) == 1)
activate_source(child); activate_source(child);
UNUSED_PARAMETER(parent); UNUSED_PARAMETER(parent);
@ -390,7 +391,7 @@ static void activate_tree(obs_source_t parent, obs_source_t child, void *param)
static void deactivate_tree(obs_source_t parent, obs_source_t child, static void deactivate_tree(obs_source_t parent, obs_source_t child,
void *param) void *param)
{ {
if (--child->activate_refs == 0) if (os_atomic_dec_long(&child->activate_refs) == 0)
deactivate_source(child); deactivate_source(child);
UNUSED_PARAMETER(parent); UNUSED_PARAMETER(parent);
@ -399,7 +400,7 @@ static void deactivate_tree(obs_source_t parent, obs_source_t child,
static void show_tree(obs_source_t parent, obs_source_t child, void *param) static void show_tree(obs_source_t parent, obs_source_t child, void *param)
{ {
if (++child->show_refs == 1) if (os_atomic_inc_long(&child->show_refs) == 1)
show_source(child); show_source(child);
UNUSED_PARAMETER(parent); UNUSED_PARAMETER(parent);
@ -408,7 +409,7 @@ static void show_tree(obs_source_t parent, obs_source_t child, void *param)
static void hide_tree(obs_source_t parent, obs_source_t child, void *param) static void hide_tree(obs_source_t parent, obs_source_t child, void *param)
{ {
if (--child->show_refs == 0) if (os_atomic_dec_long(&child->show_refs) == 0)
hide_source(child); hide_source(child);
UNUSED_PARAMETER(parent); UNUSED_PARAMETER(parent);
@ -419,13 +420,13 @@ void obs_source_activate(obs_source_t source, enum view_type type)
{ {
if (!source) return; if (!source) return;
if (++source->show_refs == 1) { if (os_atomic_inc_long(&source->show_refs) == 1) {
show_source(source); show_source(source);
obs_source_enum_tree(source, show_tree, NULL); obs_source_enum_tree(source, show_tree, NULL);
} }
if (type == MAIN_VIEW) { if (type == MAIN_VIEW) {
if (++source->activate_refs == 1) { if (os_atomic_inc_long(&source->activate_refs) == 1) {
activate_source(source); activate_source(source);
obs_source_enum_tree(source, activate_tree, NULL); obs_source_enum_tree(source, activate_tree, NULL);
obs_source_set_present_volume(source, 1.0f); obs_source_set_present_volume(source, 1.0f);
@ -437,13 +438,13 @@ void obs_source_deactivate(obs_source_t source, enum view_type type)
{ {
if (!source) return; if (!source) return;
if (--source->show_refs == 0) { if (os_atomic_dec_long(&source->show_refs) == 0) {
hide_source(source); hide_source(source);
obs_source_enum_tree(source, hide_tree, NULL); obs_source_enum_tree(source, hide_tree, NULL);
} }
if (type == MAIN_VIEW) { if (type == MAIN_VIEW) {
if (--source->activate_refs == 0) { if (os_atomic_dec_long(&source->activate_refs) == 0) {
deactivate_source(source); deactivate_source(source);
obs_source_enum_tree(source, deactivate_tree, NULL); obs_source_enum_tree(source, deactivate_tree, NULL);
obs_source_set_present_volume(source, 0.0f); obs_source_set_present_volume(source, 0.0f);
@ -496,7 +497,7 @@ static inline void handle_ts_jump(obs_source_t source, uint64_t expected,
/* if has video, ignore audio data until reset */ /* if has video, ignore audio data until reset */
if (source->info.output_flags & OBS_SOURCE_ASYNC_VIDEO) if (source->info.output_flags & OBS_SOURCE_ASYNC_VIDEO)
source->audio_reset_ref--; os_atomic_dec_long(&source->audio_reset_ref);
else else
reset_audio_timing(source, ts); reset_audio_timing(source, ts);
} }
@ -1384,12 +1385,12 @@ static void enum_source_tree_callback(obs_source_t parent, obs_source_t child,
struct source_enum_data *data = param; struct source_enum_data *data = param;
if (child->info.enum_sources && !child->enum_refs) { if (child->info.enum_sources && !child->enum_refs) {
child->enum_refs++; os_atomic_inc_long(&child->enum_refs);
child->info.enum_sources(child->data, child->info.enum_sources(child->data,
enum_source_tree_callback, data); enum_source_tree_callback, data);
child->enum_refs--; os_atomic_dec_long(&child->enum_refs);
} }
data->enum_callback(parent, child, data->param); data->enum_callback(parent, child, data->param);
@ -1404,9 +1405,9 @@ void obs_source_enum_sources(obs_source_t source,
obs_source_addref(source); obs_source_addref(source);
source->enum_refs++; os_atomic_inc_long(&source->enum_refs);
source->info.enum_sources(source->data, enum_callback, param); source->info.enum_sources(source->data, enum_callback, param);
source->enum_refs--; os_atomic_dec_long(&source->enum_refs);
obs_source_release(source); obs_source_release(source);
} }
@ -1422,10 +1423,10 @@ void obs_source_enum_tree(obs_source_t source,
obs_source_addref(source); obs_source_addref(source);
source->enum_refs++; os_atomic_inc_long(&source->enum_refs);
source->info.enum_sources(source->data, enum_source_tree_callback, source->info.enum_sources(source->data, enum_source_tree_callback,
&data); &data);
source->enum_refs--; os_atomic_dec_long(&source->enum_refs);
obs_source_release(source); obs_source_release(source);
} }

View File

@ -116,13 +116,3 @@ int os_mkdir(const char *path)
return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR; return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
} }
long atomic_inc_long(volatile long *val)
{
return __sync_fetch_and_add(val, 1);
}
long atomic_dec_long(volatile long *val)
{
return __sync_fetch_and_sub(val, 1);
}

View File

@ -185,16 +185,6 @@ int os_mkdir(const char *path)
return MKDIR_SUCCESS; return MKDIR_SUCCESS;
} }
long atomic_inc_long(volatile long *val)
{
return InterlockedIncrement(val);
}
long atomic_dec_long(volatile long *val)
{
return InterlockedDecrement(val);
}
BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved) BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
{ {

View File

@ -76,9 +76,6 @@ EXPORT char *os_get_config_path(const char *name);
EXPORT bool os_file_exists(const char *path); EXPORT bool os_file_exists(const char *path);
EXPORT long atomic_inc_long(volatile long *val);
EXPORT long atomic_dec_long(volatile long *val);
#define MKDIR_EXISTS 1 #define MKDIR_EXISTS 1
#define MKDIR_SUCCESS 0 #define MKDIR_SUCCESS 0
#define MKDIR_ERROR -1 #define MKDIR_ERROR -1

View File

@ -235,3 +235,13 @@ int os_sem_wait(os_sem_t sem)
} }
#endif #endif
long os_atomic_inc_long(volatile long *val)
{
return __sync_fetch_and_add(val, 1);
}
long os_atomic_dec_long(volatile long *val)
{
return __sync_fetch_and_sub(val, 1);
}

View File

@ -150,3 +150,13 @@ int os_sem_wait(os_sem_t sem)
ret = WaitForSingleObject(sem->handle, INFINITE); ret = WaitForSingleObject(sem->handle, INFINITE);
return (ret == WAIT_OBJECT_0) ? 0 : -1; return (ret == WAIT_OBJECT_0) ? 0 : -1;
} }
long os_atomic_inc_long(volatile long *val)
{
return InterlockedIncrement(val);
}
long os_atomic_dec_long(volatile long *val)
{
return InterlockedDecrement(val);
}

View File

@ -67,6 +67,9 @@ EXPORT void os_sem_destroy(os_sem_t sem);
EXPORT int os_sem_post(os_sem_t sem); EXPORT int os_sem_post(os_sem_t sem);
EXPORT int os_sem_wait(os_sem_t sem); EXPORT int os_sem_wait(os_sem_t sem);
EXPORT long os_atomic_inc_long(volatile long *val);
EXPORT long os_atomic_dec_long(volatile long *val);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -5,7 +5,7 @@
static inline void init_textures(struct dc_capture *capture) static inline void init_textures(struct dc_capture *capture)
{ {
for (size_t i = 0; i < capture->num_textures; i++) { for (int i = 0; i < capture->num_textures; i++) {
if (capture->compatibility) if (capture->compatibility)
capture->textures[i] = gs_create_texture( capture->textures[i] = gs_create_texture(
capture->width, capture->height, capture->width, capture->height,
@ -28,6 +28,8 @@ void dc_capture_init(struct dc_capture *capture, int x, int y,
uint32_t width, uint32_t height, bool cursor, uint32_t width, uint32_t height, bool cursor,
bool compatibility) bool compatibility)
{ {
memset(capture, 0, sizeof(struct dc_capture));
capture->x = x; capture->x = x;
capture->y = y; capture->y = y;
capture->width = width; capture->width = width;
@ -76,7 +78,7 @@ void dc_capture_free(struct dc_capture *capture)
gs_entercontext(obs_graphics()); gs_entercontext(obs_graphics());
for (size_t i = 0; i < capture->num_textures; i++) for (int i = 0; i < capture->num_textures; i++)
texture_destroy(capture->textures[i]); texture_destroy(capture->textures[i]);
gs_leavecontext(); gs_leavecontext();