Statistics
| Branch: | Revision:

ffmpeg / libavcodec / os2thread.c @ 755bfeab

History | View | Annotate | Download (4.24 KB)

1
/*
2
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg 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 GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 *
20
 */
21
//#define DEBUG
22

    
23
// Ported by Vlad Stelmahovsky
24

    
25
#include "avcodec.h"
26

    
27
#define INCL_DOS
28
#define INCL_DOSERRORS
29
#define INCL_DOSDEVIOCTL
30
#include <os2.h>
31

    
32
typedef struct ThreadContext{
33
    AVCodecContext *avctx;
34
    int thread;
35
    HEV work_sem;
36
    HEV done_sem;
37
    int (*func)(AVCodecContext *c, void *arg);
38
    void *arg;
39
    int ret;
40
}ThreadContext;
41

    
42

    
43
void thread_func(void *v){
44
    ThreadContext *c= v;
45

    
46
    for(;;){
47
        //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
48
        DosWaitEventSem(c->work_sem, SEM_INDEFINITE_WAIT);
49
//        WaitForSingleObject(c->work_sem, INFINITE);
50
//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
51
        if(c->func)
52
            c->ret= c->func(c->avctx, c->arg);
53
        else
54
            return;
55
        //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
56
        DosPostEventSem(c->done_sem);
57
//        ReleaseSemaphore(c->done_sem, 1, 0);
58
    }
59

    
60
    return;
61
}
62

    
63
/**
64
 * free what has been allocated by avcodec_thread_init().
65
 * must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running
66
 */
67
void avcodec_thread_free(AVCodecContext *s){
68
    ThreadContext *c= s->thread_opaque;
69
    int i;
70

    
71
    for(i=0; i<s->thread_count; i++){
72

    
73
        c[i].func= NULL;
74
        DosPostEventSem(c[i].work_sem);
75
        //        ReleaseSemaphore(c[i].work_sem, 1, 0);
76
        DosWaitThread((PTID)&c[i].thread,DCWW_WAIT);
77
//        WaitForSingleObject(c[i].thread, INFINITE);
78
        if(c[i].work_sem) DosCloseEventSem(c[i].work_sem);//CloseHandle(c[i].work_sem);
79
        if(c[i].done_sem) DosCloseEventSem(c[i].done_sem);//CloseHandle(c[i].done_sem);
80
    }
81

    
82
    av_freep(&s->thread_opaque);
83
}
84

    
85
int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
86
    ThreadContext *c= s->thread_opaque;
87
    int i;
88

    
89
    assert(s == c->avctx);
90
    assert(count <= s->thread_count);
91

    
92
    /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
93

    
94
    for(i=0; i<count; i++){
95

    
96
        c[i].arg= arg[i];
97
        c[i].func= func;
98
        c[i].ret= 12345;
99

    
100
        DosPostEventSem(c[i].work_sem);
101
//        ReleaseSemaphore(c[i].work_sem, 1, 0);
102
    }
103
    for(i=0; i<count; i++){
104
        DosWaitEventSem(c[i].done_sem,SEM_INDEFINITE_WAIT);
105
//        WaitForSingleObject(c[i].done_sem, INFINITE);
106

    
107
        c[i].func= NULL;
108
        if(ret) ret[i]= c[i].ret;
109
    }
110
    return 0;
111
}
112

    
113
int avcodec_thread_init(AVCodecContext *s, int thread_count){
114
    int i;
115
    ThreadContext *c;
116
    uint32_t threadid;
117

    
118
    s->thread_count= thread_count;
119

    
120
    assert(!s->thread_opaque);
121
    c= av_mallocz(sizeof(ThreadContext)*thread_count);
122
    s->thread_opaque= c;
123

    
124
    for(i=0; i<thread_count; i++){
125
//printf("init semaphors %d\n", i); fflush(stdout);
126
        c[i].avctx= s;
127

    
128
        if (DosCreateEventSem(NULL,&c[i].work_sem,DC_SEM_SHARED,0))
129
            goto fail;
130
        if (DosCreateEventSem(NULL,&c[i].done_sem,DC_SEM_SHARED,0))
131
            goto fail;
132

    
133
//printf("create thread %d\n", i); fflush(stdout);
134
//        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
135
        c[i].thread = _beginthread(thread_func, NULL, 0x10000, &c[i]);
136
        if( c[i].thread <= 0 ) goto fail;
137
    }
138
//printf("init done\n"); fflush(stdout);
139

    
140
    s->execute= avcodec_thread_execute;
141

    
142
    return 0;
143
fail:
144
    avcodec_thread_free(s);
145
    return -1;
146
}