grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / camd / camdwait.c
blobde41955f60162623dc77471e639513623c13f99e
1 /*
2 Copyright © 1995-2004, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <proto/exec.h>
8 #include <dos/dostags.h>
10 #include "camd_intern.h"
12 # undef DEBUG
13 # define DEBUG 1
14 # include AROS_DEBUG_H_FILE
17 struct SignalSemaphore camdwaitsemaphore={{0}};
18 struct SignalSemaphore camdwaitsemaphore2={{0}};
20 struct Task *camdwaittask;
21 ULONG camdwaitsig;
22 ULONG camdwaitsig2;
24 ULONG camdwaitprocstatus=0; //0=not alive or not initialized, 1=alive and fine, 2=failed, 3=dead.
27 SAVEDS void CamdTimerProc(void){
28 struct timerequest *TimerIO;
29 struct MsgPort *TimerMP;
30 ULONG sig;
31 int error;
33 camdwaitsig=AllocSignal(-1);
34 if(camdwaitsig==-1){
35 camdwaitprocstatus=2;
36 return;
38 camdwaitsig2=AllocSignal(-1);
39 if(camdwaitsig2==-1){
40 FreeSignal(1L<<camdwaitsig);
41 camdwaitprocstatus=2;
42 return;
45 TimerMP=CreateMsgPort();
46 if(TimerMP==NULL){
47 FreeSignal(1L<<camdwaitsig2);
48 FreeSignal(1L<<camdwaitsig);
49 camdwaitprocstatus=2;
50 return;
53 TimerIO=(struct timerequest *)AllocMem(sizeof(struct timerequest),MEMF_ANY|MEMF_CLEAR|MEMF_PUBLIC);
55 if(TimerIO==NULL){
56 FreeSignal(1L<<camdwaitsig2);
57 FreeSignal(1L<<camdwaitsig);
58 DeleteMsgPort(TimerMP);
59 camdwaitprocstatus=2;
60 return;
63 TimerIO->tr_node.io_Message.mn_Node.ln_Type=NT_MESSAGE;
64 TimerIO->tr_node.io_Message.mn_ReplyPort=TimerMP;
65 TimerIO->tr_node.io_Message.mn_Length=sizeof(struct timerequest);
67 /* No support for eclock in AROS. */
68 #ifndef __AROS__
69 if((error=OpenDevice(
70 TIMERNAME,UNIT_ECLOCK,(struct IORequest *)TimerIO,0L
71 ))!=0){
72 #else
73 if((error=OpenDevice(
74 TIMERNAME,UNIT_VBLANK,(struct IORequest *)TimerIO,0L
75 ))!=0){
76 #endif
78 FreeSignal(1L<<camdwaitsig2);
79 FreeSignal(1L<<camdwaitsig);
80 TimerIO->tr_node.io_Message.mn_Node.ln_Type=(UBYTE)-1;
81 TimerIO->tr_node.io_Device=(struct Device *)-1L;
82 TimerIO->tr_node.io_Unit=(struct Unit *)-1L;
83 FreeMem(TimerIO,sizeof(struct timerequest));
84 DeleteMsgPort(TimerMP);
85 camdwaitprocstatus=2;
86 return;
89 ObtainSemaphore(&camdwaitsemaphore);
91 camdwaittask=FindTask(0L);
93 camdwaitprocstatus=1;
95 for(;;){
96 sig=Wait(
97 1L<<camdwaitsig |
98 SIGBREAKF_CTRL_C
100 if(sig&SIGBREAKF_CTRL_C) break;
102 if(sig & 1L<<camdwaitsig){ // Someone wants to obtain the semaphore?
103 TimerIO->tr_node.io_Command=TR_ADDREQUEST;
104 TimerIO->tr_time.tv_secs=0;
105 TimerIO->tr_time.tv_micro=10;
106 DoIO((struct IORequest *)TimerIO); // ..Better let them wait.
108 ReleaseSemaphore(&camdwaitsemaphore); // Well, okey then.
110 Wait(1L<<camdwaitsig2); // Finished soon?
112 ObtainSemaphore(&camdwaitsemaphore); // But I want it back!
117 ReleaseSemaphore(&camdwaitsemaphore);
119 FreeSignal(1L<<camdwaitsig2);
120 FreeSignal(1L<<camdwaitsig);
121 CloseDevice((struct IORequest *)TimerIO);
122 TimerIO->tr_node.io_Message.mn_Node.ln_Type=(UBYTE)-1;
123 TimerIO->tr_node.io_Device=(struct Device *)-1L;
124 TimerIO->tr_node.io_Unit=(struct Unit *)-1L;
125 FreeMem(TimerIO,sizeof(struct timerequest));
127 DeleteMsgPort(TimerMP);
129 camdwaitprocstatus=3;
131 return;
134 BOOL InitCamdTimer(void){
135 struct Process *process;
137 InitSemaphore(&camdwaitsemaphore);
138 InitSemaphore(&camdwaitsemaphore2);
140 process=CreateNewProcTags(
141 NP_Entry, (IPTR) CamdTimerProc,
142 NP_Name, (IPTR) "Camd Wait Proc",
143 NP_Priority,5,
144 TAG_END
146 if(process==NULL) return FALSE;
148 while(camdwaitprocstatus==0) Delay(1);
150 if(camdwaitprocstatus==2) return FALSE;
152 return TRUE;
155 void UninitCamdTimer(void){
157 if(camdwaitprocstatus==2) return;
159 Signal(camdwaittask,SIGBREAKF_CTRL_C);
160 while(camdwaitprocstatus!=3) Delay(1);
166 * When a hardware-sender buffer is full, or we are sending a sysex-message
167 * to hardware, we need to wait for small amounts of time. That is what
168 * this function tries to do.
172 void CamdWait(void){
174 static BOOL iswaiting=FALSE;
175 BOOL imjustgoingtoreturnsoon=iswaiting;
177 ObtainSemaphore(&camdwaitsemaphore2);
179 if(imjustgoingtoreturnsoon==TRUE){ // No big point having more than one visitor to wait.
180 ReleaseSemaphore(&camdwaitsemaphore2);
181 return;
184 iswaiting=TRUE;
186 Signal(camdwaittask,1L<<camdwaitsig); // Give me the semaphore!
187 ObtainSemaphore(&camdwaitsemaphore); // This should take some time.
188 Signal(camdwaittask,1L<<camdwaitsig2); // Okey, I've got it.
189 ReleaseSemaphore(&camdwaitsemaphore); // You're welcome.
191 iswaiting=FALSE;
193 ReleaseSemaphore(&camdwaitsemaphore2);
195 return;