1
/* Copyright (C) 2002, 2003 Vincent Penquerc'h.
2
This file is part of the alogg library.
3
Written by Vincent Penquerc'h <lyrian -at- kezako -dot- net>.
5
The alogg library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
10
The alogg library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
15
You should have received a copy of the GNU Lesser General Public
16
License along with the alogg Library; if not, write to the Free
17
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20
As a special exception, if you link this library statically with a
21
program, you do not have to distribute the object files for this
23
This exception does not however invalidate any other reasons why
24
you would have to distribute the object files for this program. */
28
#include <sys/types.h>
31
#include <allegro/debug.h>
34
typedef struct alogg_thread {
36
pthread_mutex_t mutex;
37
struct alogg_stream *stream;
43
static void *alogg_threaded_streamer(void *data)
46
struct alogg_thread *thread=data;
48
ASSERT(thread->stream);
51
struct timeval timeout;
53
alogg_lock_thread(thread);
54
ret=alogg_update_streaming(thread->stream);
55
alogg_unlock_thread(thread);
57
if (ret==0 || thread->stop) break;
61
select(0,NULL,NULL,NULL,&timeout);
64
TRACE("stopping stream\n");
65
alogg_lock_thread(thread);
68
alogg_stop_streaming(thread->stream);
69
alogg_unlock_thread(thread);
70
pthread_mutex_destroy(&thread->mutex);
71
pthread_exit(&thread->thread);
75
int alogg_lock_thread(struct alogg_thread *thread)
78
return pthread_mutex_lock(&thread->mutex);
81
int alogg_try_lock_thread(struct alogg_thread *thread)
84
return pthread_mutex_trylock(&thread->mutex);
87
int alogg_unlock_thread(struct alogg_thread *thread)
90
return pthread_mutex_unlock(&thread->mutex);
93
int alogg_is_thread_alive(struct alogg_thread *thread)
96
if (!thread) return 0;
100
struct alogg_thread *alogg_create_thread(struct alogg_stream *stream)
102
alogg_thread *thread=NULL;
106
/* Create a control structure */
107
thread=malloc(sizeof(alogg_thread));
108
if (!thread) return NULL;
110
thread->stream=stream;
114
/* Initialize a new thread and its associated mutex */
115
pthread_mutex_init(&thread->mutex,NULL);
116
if (pthread_create(&thread->thread,NULL,&alogg_threaded_streamer,thread)) {
117
pthread_mutex_destroy(&thread->mutex);
125
void alogg_destroy_thread(alogg_thread *thread)
130
ASSERT(!thread->alive);
135
void alogg_stop_thread(alogg_thread *thread)
140
alogg_lock_thread(thread);
142
alogg_unlock_thread(thread);
145
struct alogg_stream *alogg_get_thread_stream(alogg_thread *thread)
149
if (!thread) return NULL;
150
if (!thread->alive) return NULL;
151
return thread->stream;