~ubuntu-branches/ubuntu/karmic/alogg/karmic

« back to all changes in this revision

Viewing changes to aloggpth.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Pineau
  • Date: 2004-02-15 23:01:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040215230145-6ij4xz3m3t2zijg4
Tags: upstream-1.3.3
ImportĀ upstreamĀ versionĀ 1.3.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>.
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
   02111-1307 USA.
 
19
 
 
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
 
22
   program.
 
23
   This exception does not however invalidate any other reasons why
 
24
   you would have to distribute the object files for this program.  */
 
25
 
 
26
 
 
27
#include <sys/time.h>
 
28
#include <sys/types.h>
 
29
#include <unistd.h>
 
30
#include <pthread.h>
 
31
#include <allegro/debug.h>
 
32
#include "aloggpth.h"
 
33
 
 
34
typedef struct alogg_thread {
 
35
  pthread_t thread;
 
36
  pthread_mutex_t mutex;
 
37
  struct alogg_stream *stream;
 
38
  int stop;
 
39
  int alive;
 
40
} alogg_thread;
 
41
 
 
42
 
 
43
static void *alogg_threaded_streamer(void *data)
 
44
{
 
45
  int ret=1;
 
46
  struct alogg_thread *thread=data;
 
47
  ASSERT(thread);
 
48
  ASSERT(thread->stream);
 
49
 
 
50
  while (1) {
 
51
    struct timeval timeout;
 
52
 
 
53
    alogg_lock_thread(thread);
 
54
    ret=alogg_update_streaming(thread->stream);
 
55
    alogg_unlock_thread(thread);
 
56
 
 
57
    if (ret==0 || thread->stop) break;
 
58
 
 
59
    timeout.tv_sec=0;
 
60
    timeout.tv_usec=100;
 
61
    select(0,NULL,NULL,NULL,&timeout);
 
62
  }
 
63
 
 
64
  TRACE("stopping stream\n");
 
65
  alogg_lock_thread(thread);
 
66
  thread->alive=0;
 
67
  thread->stop=1;
 
68
  alogg_stop_streaming(thread->stream);
 
69
  alogg_unlock_thread(thread);
 
70
  pthread_mutex_destroy(&thread->mutex);
 
71
  pthread_exit(&thread->thread);
 
72
  return NULL;
 
73
}
 
74
 
 
75
int alogg_lock_thread(struct alogg_thread *thread)
 
76
{
 
77
  ASSERT(thread);
 
78
  return pthread_mutex_lock(&thread->mutex);
 
79
}
 
80
 
 
81
int alogg_try_lock_thread(struct alogg_thread *thread)
 
82
{
 
83
  ASSERT(thread);
 
84
  return pthread_mutex_trylock(&thread->mutex);
 
85
}
 
86
 
 
87
int alogg_unlock_thread(struct alogg_thread *thread)
 
88
{
 
89
  ASSERT(thread);
 
90
  return pthread_mutex_unlock(&thread->mutex);
 
91
}
 
92
 
 
93
int alogg_is_thread_alive(struct alogg_thread *thread)
 
94
{
 
95
  ASSERT(thread);
 
96
  if (!thread) return 0;
 
97
  return thread->alive;
 
98
}
 
99
 
 
100
struct alogg_thread *alogg_create_thread(struct alogg_stream *stream)
 
101
{
 
102
  alogg_thread *thread=NULL;
 
103
 
 
104
  ASSERT(stream);
 
105
 
 
106
  /* Create a control structure */
 
107
  thread=malloc(sizeof(alogg_thread));
 
108
  if (!thread) return NULL;
 
109
 
 
110
  thread->stream=stream;
 
111
  thread->stop=0;
 
112
  thread->alive=1;
 
113
 
 
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);
 
118
    free(thread);
 
119
    return NULL;
 
120
  }
 
121
 
 
122
  return thread;
 
123
}
 
124
 
 
125
void alogg_destroy_thread(alogg_thread *thread)
 
126
{
 
127
  ASSERT(thread);
 
128
 
 
129
  if (!thread) return;
 
130
  ASSERT(!thread->alive);
 
131
 
 
132
  free(thread);
 
133
}
 
134
 
 
135
void alogg_stop_thread(alogg_thread *thread)
 
136
{
 
137
  ASSERT(thread);
 
138
 
 
139
  if (!thread) return;
 
140
  alogg_lock_thread(thread);
 
141
  thread->stop=1;
 
142
  alogg_unlock_thread(thread);
 
143
}
 
144
 
 
145
struct alogg_stream *alogg_get_thread_stream(alogg_thread *thread)
 
146
{
 
147
  ASSERT(thread);
 
148
 
 
149
  if (!thread) return NULL;
 
150
  if (!thread->alive) return NULL;
 
151
  return thread->stream;
 
152
}