Commit bba54db0 authored by Eric Pouech's avatar Eric Pouech Committed by Alexandre Julliard

No longer using global vars (shared data) for mm timers, but a

specific structure for each attached process.
parent 2858ac99
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#define __WINE_MULTIMEDIA_H #define __WINE_MULTIMEDIA_H
#include "mmsystem.h" #include "mmsystem.h"
#include "winbase.h"
#define MAX_MIDIINDRV (16) #define MAX_MIDIINDRV (16)
/* For now I'm making 16 the maximum number of midi devices one can /* For now I'm making 16 the maximum number of midi devices one can
...@@ -84,6 +85,33 @@ typedef enum { ...@@ -84,6 +85,33 @@ typedef enum {
MCI_MAP_PASS /* ok, no memory allocated. to be sent to 32 bit proc */ MCI_MAP_PASS /* ok, no memory allocated. to be sent to 32 bit proc */
} MCI_MapType; } MCI_MapType;
typedef struct tagTIMERENTRY {
UINT wDelay;
UINT wResol;
FARPROC16 lpFunc;
DWORD dwUser;
UINT wFlags;
UINT wTimerID;
UINT wCurTime;
UINT isWin32;
struct tagTIMERENTRY* lpNext;
} TIMERENTRY, *LPTIMERENTRY;
typedef struct tagWINE_MM_IDATA {
/* iData reference */
DWORD dwThisProcess;
struct tagWINE_MM_IDATA* lpNextIData;
/* winmm part */
HANDLE hWinMM32Instance;
HANDLE hWinMM16Instance;
HANDLE h16Module32;
/* mm timer part */
HANDLE hMMTimer;
DWORD mmSysTimeMS;
LPTIMERENTRY lpTimerList;
CRITICAL_SECTION cs;
} WINE_MM_IDATA, *LPWINE_MM_IDATA;
/* function prototypes */ /* function prototypes */
#define MCI_GetDrv(wDevID) (&mciDrv[MCI_DevIDToIndex(wDevID)]) #define MCI_GetDrv(wDevID) (&mciDrv[MCI_DevIDToIndex(wDevID)])
...@@ -136,8 +164,9 @@ HANDLE16 WINAPI mmThreadGetTask16(HANDLE16 hndl); ...@@ -136,8 +164,9 @@ HANDLE16 WINAPI mmThreadGetTask16(HANDLE16 hndl);
BOOL16 WINAPI mmThreadIsValid16(HANDLE16 hndl); BOOL16 WINAPI mmThreadIsValid16(HANDLE16 hndl);
BOOL16 WINAPI mmThreadIsCurrent16(HANDLE16 hndl); BOOL16 WINAPI mmThreadIsCurrent16(HANDLE16 hndl);
BOOL MULTIMEDIA_MMTimeInit(void);
BOOL MULTIMEDIA_MciInit(void); BOOL MULTIMEDIA_MciInit(void);
LPWINE_MM_IDATA MULTIMEDIA_GetIData(void);
BOOL MULTIMEDIA_MidiInit(void); BOOL MULTIMEDIA_MidiInit(void);
#endif /* __WINE_MULTIMEDIA_H */ #endif /* __WINE_MULTIMEDIA_H */
...@@ -6,36 +6,17 @@ ...@@ -6,36 +6,17 @@
* Copyright 1993 Martin Ayotte * Copyright 1993 Martin Ayotte
*/ */
#include <stdlib.h>
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include "winbase.h" #include "winbase.h"
#include "wine/winbase16.h" /* GetTaskDS */
#include "callback.h" #include "callback.h"
#include "mmsystem.h" #include "multimedia.h"
#include "services.h" #include "services.h"
#include "syslevel.h"
#include "debugtools.h" #include "debugtools.h"
DEFAULT_DEBUG_CHANNEL(mmtime) DEFAULT_DEBUG_CHANNEL(mmtime)
typedef struct tagTIMERENTRY {
UINT wDelay;
UINT wResol;
FARPROC16 lpFunc;
HINSTANCE hInstance;
DWORD dwUser;
UINT wFlags;
UINT wTimerID;
UINT wCurTime;
UINT isWin32;
struct tagTIMERENTRY* lpNext;
} TIMERENTRY, *LPTIMERENTRY;
static DWORD mmSysTimeMS;
static LPTIMERENTRY lpTimerList = NULL;
static HANDLE hMMTimer = 0;
CRITICAL_SECTION mmTimeCritSect;
/* /*
* FIXME * FIXME
* We're using "1" as the mininum resolution to the timer, * We're using "1" as the mininum resolution to the timer,
...@@ -51,7 +32,6 @@ static void TIME_TriggerCallBack(LPTIMERENTRY lpTimer, DWORD dwCurrent) ...@@ -51,7 +32,6 @@ static void TIME_TriggerCallBack(LPTIMERENTRY lpTimer, DWORD dwCurrent)
TRACE("before CallBack (%lu)!\n", dwCurrent); TRACE("before CallBack (%lu)!\n", dwCurrent);
TRACE("lpFunc=%p wTimerID=%04X dwUser=%08lX !\n", TRACE("lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser); lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
TRACE("hInstance=%04X !\n", lpTimer->hInstance);
/* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
* during interrupt time, is allowed to execute very limited number of API calls (like * during interrupt time, is allowed to execute very limited number of API calls (like
...@@ -84,19 +64,31 @@ static void TIME_TriggerCallBack(LPTIMERENTRY lpTimer, DWORD dwCurrent) ...@@ -84,19 +64,31 @@ static void TIME_TriggerCallBack(LPTIMERENTRY lpTimer, DWORD dwCurrent)
/************************************************************************** /**************************************************************************
* TIME_MMSysTimeCallback * TIME_MMSysTimeCallback
*/ */
static void CALLBACK TIME_MMSysTimeCallback( ULONG_PTR dummy ) static void CALLBACK TIME_MMSysTimeCallback(ULONG_PTR ptr_)
{ {
LPTIMERENTRY lpTimer, lpNextTimer; LPTIMERENTRY lpTimer, lpNextTimer;
LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)ptr_;
iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
/* this is a hack...
* since timeSetEvent() and timeKillEvent() can be called
* from 16 bit code, there are cases where win16 lock is
* locked upon entering timeSetEvent(), and then the process
* heap is locked. OTOH, in this callback the process heap
* is first locked, and the win16 lock can be locked while
* calling a client callback. to avoid deadlocks, the process
* heap and win 16 lock must always be locked in the same
* order...
*/
SYSLEVEL_EnterWin16Lock();
EnterCriticalSection(&iData->cs);
mmSysTimeMS += MMSYSTIME_MININTERVAL; for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
EnterCriticalSection(&mmTimeCritSect);
for (lpTimer = lpTimerList; lpTimer != NULL; ) {
lpNextTimer = lpTimer->lpNext; lpNextTimer = lpTimer->lpNext;
if (lpTimer->wCurTime < MMSYSTIME_MININTERVAL) { if (lpTimer->wCurTime < MMSYSTIME_MININTERVAL) {
lpTimer->wCurTime = lpTimer->wDelay; lpTimer->wCurTime = lpTimer->wDelay;
TIME_TriggerCallBack(lpTimer, mmSysTimeMS); TIME_TriggerCallBack(lpTimer, iData->mmSysTimeMS);
if (lpTimer->wFlags & TIME_ONESHOT) if (lpTimer->wFlags & TIME_ONESHOT)
timeKillEvent(lpTimer->wTimerID); timeKillEvent(lpTimer->wTimerID);
} else { } else {
...@@ -105,35 +97,36 @@ static void CALLBACK TIME_MMSysTimeCallback( ULONG_PTR dummy ) ...@@ -105,35 +97,36 @@ static void CALLBACK TIME_MMSysTimeCallback( ULONG_PTR dummy )
lpTimer = lpNextTimer; lpTimer = lpNextTimer;
} }
LeaveCriticalSection(&mmTimeCritSect); LeaveCriticalSection(&iData->cs);
SYSLEVEL_LeaveWin16Lock();
} }
/************************************************************************** /**************************************************************************
* MULTIMEDIA_MMTimeInit [internal] * MULTIMEDIA_MMTimeStart [internal]
*/ */
BOOL MULTIMEDIA_MMTimeInit(void) static LPWINE_MM_IDATA MULTIMEDIA_MMTimeStart(void)
{ {
InitializeCriticalSection(&mmTimeCritSect); LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
MakeCriticalSectionGlobal(&mmTimeCritSect);
return TRUE;
}
/************************************************************************** if (!iData || IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
* MULTIMEDIA_MMTimeStart [internal] ERR("iData is not correctly set, please report. Expect failure.\n");
return 0;
}
/* FIXME: the service timer is never killed, even when process is
* detached from this DLL
*/ */
static BOOL MULTIMEDIA_MMTimeStart(void)
{
/* one could think it's possible to stop the service thread activity when no more /* one could think it's possible to stop the service thread activity when no more
* mm timers are active, but this would require to handle delta (in time) between * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
* GetTickCount() and timeGetTime() which are not -currently- synchronized. * without being incremented within the service thread callback.
*/ */
if (!hMMTimer) { if (!iData->hMMTimer) {
mmSysTimeMS = GetTickCount(); iData->mmSysTimeMS = GetTickCount();
hMMTimer = SERVICE_AddTimer( MMSYSTIME_MININTERVAL*1000L, TIME_MMSysTimeCallback, 0 ); iData->lpTimerList = NULL;
iData->hMMTimer = SERVICE_AddTimer(MMSYSTIME_MININTERVAL*1000L, TIME_MMSysTimeCallback, (DWORD)iData);
InitializeCriticalSection(&iData->cs);
} }
return TRUE; return iData;
} }
/************************************************************************** /**************************************************************************
...@@ -141,14 +134,18 @@ static BOOL MULTIMEDIA_MMTimeStart(void) ...@@ -141,14 +134,18 @@ static BOOL MULTIMEDIA_MMTimeStart(void)
*/ */
MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize) MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
{ {
LPWINE_MM_IDATA iData;
TRACE("(%p, %u);\n", lpTime, wSize); TRACE("(%p, %u);\n", lpTime, wSize);
if (wSize >= sizeof(*lpTime)) { if (wSize >= sizeof(*lpTime)) {
MULTIMEDIA_MMTimeStart(); iData = MULTIMEDIA_MMTimeStart();
if (!iData)
return MMSYSERR_NOMEM;
lpTime->wType = TIME_MS; lpTime->wType = TIME_MS;
lpTime->u.ms = mmSysTimeMS; lpTime->u.ms = iData->mmSysTimeMS;
TRACE("=> %lu\n", mmSysTimeMS); TRACE("=> %lu\n", iData->mmSysTimeMS);
} }
return 0; return 0;
...@@ -159,14 +156,18 @@ MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize) ...@@ -159,14 +156,18 @@ MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
*/ */
MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize) MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
{ {
LPWINE_MM_IDATA iData;
TRACE("(%p, %u);\n", lpTime, wSize); TRACE("(%p, %u);\n", lpTime, wSize);
if (wSize >= sizeof(*lpTime)) { if (wSize >= sizeof(*lpTime)) {
MULTIMEDIA_MMTimeStart(); iData = MULTIMEDIA_MMTimeStart();
if (!iData)
return MMSYSERR_NOMEM;
lpTime->wType = TIME_MS; lpTime->wType = TIME_MS;
lpTime->u.ms = mmSysTimeMS; lpTime->u.ms = iData->mmSysTimeMS;
TRACE("=> %lu\n", mmSysTimeMS); TRACE("=> %lu\n", iData->mmSysTimeMS);
} }
return 0; return 0;
...@@ -182,37 +183,40 @@ static WORD timeSetEventInternal(UINT wDelay, UINT wResol, ...@@ -182,37 +183,40 @@ static WORD timeSetEventInternal(UINT wDelay, UINT wResol,
WORD wNewID = 0; WORD wNewID = 0;
LPTIMERENTRY lpNewTimer; LPTIMERENTRY lpNewTimer;
LPTIMERENTRY lpTimer; LPTIMERENTRY lpTimer;
LPWINE_MM_IDATA iData;
TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags); TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
lpNewTimer = (LPTIMERENTRY)malloc(sizeof(TIMERENTRY)); lpNewTimer = (LPTIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(TIMERENTRY));
if (lpNewTimer == NULL) if (lpNewTimer == NULL)
return 0; return 0;
MULTIMEDIA_MMTimeStart(); iData = MULTIMEDIA_MMTimeStart();
if (!iData)
return 0;
lpNewTimer->wCurTime = wDelay; lpNewTimer->wCurTime = wDelay;
lpNewTimer->wDelay = wDelay; lpNewTimer->wDelay = wDelay;
lpNewTimer->wResol = wResol; lpNewTimer->wResol = wResol;
lpNewTimer->lpFunc = lpFunc; lpNewTimer->lpFunc = lpFunc;
lpNewTimer->isWin32 = isWin32; lpNewTimer->isWin32 = isWin32;
lpNewTimer->hInstance = GetTaskDS16();
lpNewTimer->dwUser = dwUser; lpNewTimer->dwUser = dwUser;
lpNewTimer->wFlags = wFlags; lpNewTimer->wFlags = wFlags;
TRACE("hInstance=%04X,lpFunc=0x%08lx !\n", lpNewTimer->hInstance, (DWORD)lpFunc); TRACE("lpFunc=0x%08lx !\n", (DWORD)lpFunc);
EnterCriticalSection(&mmTimeCritSect); EnterCriticalSection(&iData->cs);
for (lpTimer = lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) { for (lpTimer = iData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
wNewID = MAX(wNewID, lpTimer->wTimerID); wNewID = MAX(wNewID, lpTimer->wTimerID);
} }
lpNewTimer->lpNext = lpTimerList; lpNewTimer->lpNext = iData->lpTimerList;
lpTimerList = lpNewTimer; iData->lpTimerList = lpNewTimer;
lpNewTimer->wTimerID = wNewID + 1; lpNewTimer->wTimerID = wNewID + 1;
LeaveCriticalSection(&mmTimeCritSect); LeaveCriticalSection(&iData->cs);
return wNewID + 1; return wNewID + 1;
} }
...@@ -241,22 +245,23 @@ MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16 ...@@ -241,22 +245,23 @@ MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16
MMRESULT WINAPI timeKillEvent(UINT wID) MMRESULT WINAPI timeKillEvent(UINT wID)
{ {
LPTIMERENTRY* lpTimer; LPTIMERENTRY* lpTimer;
LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
MMRESULT ret = MMSYSERR_INVALPARAM; MMRESULT ret = MMSYSERR_INVALPARAM;
EnterCriticalSection(&mmTimeCritSect); EnterCriticalSection(&iData->cs);
for (lpTimer = &lpTimerList; *lpTimer; lpTimer = &((*lpTimer)->lpNext)) { for (lpTimer = &iData->lpTimerList; *lpTimer; lpTimer = &((*lpTimer)->lpNext)) {
if (wID == (*lpTimer)->wTimerID) { if (wID == (*lpTimer)->wTimerID) {
LPTIMERENTRY xlptimer = (*lpTimer)->lpNext; LPTIMERENTRY xlptimer = (*lpTimer)->lpNext;
free(*lpTimer); HeapFree(GetProcessHeap(), 0, *lpTimer);
*lpTimer = xlptimer; *lpTimer = xlptimer;
ret = TIMERR_NOERROR; ret = TIMERR_NOERROR;
break; break;
} }
} }
LeaveCriticalSection(&mmTimeCritSect); LeaveCriticalSection(&iData->cs);
return ret; return ret;
} }
...@@ -346,6 +351,5 @@ MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod) ...@@ -346,6 +351,5 @@ MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
*/ */
DWORD WINAPI timeGetTime(void) DWORD WINAPI timeGetTime(void)
{ {
MULTIMEDIA_MMTimeStart(); return MULTIMEDIA_MMTimeStart()->mmSysTimeMS;
return mmSysTimeMS;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment