Correct spelling: s/[sS]yncro/[sS]ynchro/
[vlc.git] / lib / error.c
blobf942f4d0e28db220b3e9416a85b09b0e23f798af
1 /*****************************************************************************
2 * error.c: Error handling for libvlc
3 *****************************************************************************
4 * Copyright (C) 2009 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #include "libvlc_internal.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <vlc/libvlc.h>
29 static const char oom[] = "Out of memory";
30 /* TODO: use only one thread-specific key for whole libvlc */
31 static vlc_threadvar_t context;
33 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
34 static uintptr_t refs = 0;
36 void libvlc_threads_init (void)
38 vlc_mutex_lock (&lock);
39 if (refs++ == 0)
40 vlc_threadvar_create (&context, free);
41 vlc_mutex_unlock (&lock);
44 void libvlc_threads_deinit (void)
46 vlc_mutex_lock (&lock);
47 assert (refs > 0);
48 if (--refs == 0)
49 vlc_threadvar_delete (&context);
50 vlc_mutex_unlock (&lock);
53 static char *get_error (void)
55 return vlc_threadvar_get (context);
58 static void free_error (void)
60 char *msg = get_error ();
61 if (msg != oom)
62 free (msg);
65 /**
66 * Gets a human-readable error message for the last LibVLC error in the calling
67 * thread. The resulting string is valid until another error occurs (at least
68 * until the next LibVLC call).
70 * @return NULL if there was no error, a nul-terminated string otherwise.
72 const char *libvlc_errmsg (void)
74 return get_error ();
77 /**
78 * Clears the LibVLC error status for the current thread. This is optional.
79 * By default, the error status is automatically overridden when a new error
80 * occurs, and destroyed when the thread exits.
82 void libvlc_clearerr (void)
84 free_error ();
85 vlc_threadvar_set (context, NULL);
88 /**
89 * Sets the LibVLC error status and message for the current thread.
90 * Any previous error is overridden.
91 * @return a nul terminated string (always)
93 const char *libvlc_vprinterr (const char *fmt, va_list ap)
95 char *msg;
97 assert (fmt != NULL);
98 if (vasprintf (&msg, fmt, ap) == -1)
99 msg = (char *)oom;
101 free_error ();
102 vlc_threadvar_set (context, msg);
103 return msg;
107 * Sets the LibVLC error status and message for the current thread.
108 * Any previous error is overridden.
109 * @return a nul terminated string (always)
111 const char *libvlc_printerr (const char *fmt, ...)
113 va_list ap;
114 const char *msg;
116 va_start (ap, fmt);
117 msg = libvlc_vprinterr (fmt, ap);
118 va_end (ap);
119 return msg;