Skip to content

Commit

Permalink
try to add some thread safety...perhaps that's where strange problems…
Browse files Browse the repository at this point in the history
… are coming from?
  • Loading branch information
codingismy11to7 committed Jul 31, 2006
1 parent 081895e commit 22f64af
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
39 changes: 37 additions & 2 deletions MainFrm.cpp
Expand Up @@ -166,8 +166,10 @@ CMainFrame::~CMainFrame()
void CMainFrame::timeCheck()
{
if( !m_alarmEnabled ) return;
static SYSTEMTIME time;
SYSTEMTIME time;
GetLocalTime( &time );
m_alarmsMutex.Lock();
m_snoozeTimeMutex.Lock();
std::list< DayTime::TimeAndDays* > *tmp = &(m_alarms[time.wDayOfWeek]);
if( (!tmp->empty() && tmp->front()->isIncluded( time ) )
|| (m_snoozing && m_snoozeAlarmTime.isIncluded( time )) )
Expand All @@ -185,8 +187,15 @@ void CMainFrame::timeCheck()
break;
}
}
m_snoozeTimeMutex.Unlock();
m_alarmsMutex.Unlock();
PostMessage( WM_DO_ALARM, 0, 0 );
}
else
{
m_snoozeTimeMutex.Unlock();
m_alarmsMutex.Unlock();
}
}

void CMainFrame::setVolume( UINT level )
Expand Down Expand Up @@ -302,8 +311,10 @@ LRESULT CMainFrame::DoAlarm(UINT wParam, LONG lParam)
if( tmphour > 23 ) tmphour = 0;
tmpmin -= 60;
}
m_snoozeTimeMutex.Lock();
m_snoozeAlarmTime.setTime( tmphour, tmpmin );
m_snoozeAlarmTime.day = DayTime::ALL_DAYS;
m_snoozeTimeMutex.Unlock();
SetToolTip();
}
else // stop
Expand Down Expand Up @@ -391,6 +402,7 @@ void CMainFrame::LoadReg()

m_oneAlarmEnabled = m_oneAlarmExists = false;
DayTime::AlarmList::const_iterator i, end = al.end();
m_alarmsMutex.Lock();
for( UINT j = 0; j < 7; j++ )
{
DayTime::cleanList( m_alarms[j] );
Expand Down Expand Up @@ -422,6 +434,7 @@ void CMainFrame::LoadReg()
}
}
}
m_alarmsMutex.Unlock();

//m_alarmTime.setFromBinary( ((binary)m_reg[_T("AlarmTime")]).data.c_str() );
m_shuffle = m_reg[_T("Shuffle")];
Expand All @@ -435,10 +448,12 @@ void CMainFrame::LoadReg()
m_muteOnRet = m_reg[_T("MuteOnReturn")];

m_alarmEnabled = m_reg[_T("AlarmEnabled")];
m_systrayMutex.Lock();
if( m_alarmEnabled )
m_systray.CheckMenuItem( ID_APP_ALARMENABLED, MF_CHECKED );
else
m_systray.CheckMenuItem( ID_APP_ALARMENABLED, MF_UNCHECKED );
m_systrayMutex.Unlock();

SetToolTip();
SetIcon();
Expand All @@ -447,33 +462,45 @@ void CMainFrame::LoadReg()
void CMainFrame::SetIcon()
{
if( !m_alarmEnabled || !m_oneAlarmEnabled || !m_oneAlarmExists )
{
m_systrayMutex.Lock();
m_systray.SetIcon( IDI_DISABLED );
m_systrayMutex.Unlock();
}
else
{
bool found = false;
SYSTEMTIME st;
GetLocalTime( &st );
m_alarmsMutex.Lock();
for( int i = 0; i < 8; i++ )
{
if( m_alarms[ (i + st.wDayOfWeek) % 7 ].empty() )
continue;
if( (i != 0) || (i == 0 && *(m_alarms[ (i + st.wDayOfWeek) % 7 ].front()) > DayTime::TimeAndDays( st )) )
{
m_systrayMutex.Lock();
m_systray.SetIcon( IDR_MAINFRAME );
m_systrayMutex.Unlock();
}
else
continue;
found = true;
break;
}
m_alarmsMutex.Unlock();
if( !found )
{
m_systrayMutex.Lock();
m_systray.SetIcon( IDI_DISABLED );
m_systrayMutex.Unlock();
}
}
}

void CMainFrame::SetToolTip()
{
static TCHAR buf[1024];
TCHAR buf[1024];
if( m_alarmEnabled )
{
if( !m_oneAlarmExists )
Expand All @@ -492,6 +519,8 @@ void CMainFrame::SetToolTip()
bool found = false;
SYSTEMTIME st;
GetLocalTime( &st );
m_alarmsMutex.Lock();
m_snoozeTimeMutex.Lock();
for( int i = 0; i < 8; i++ )
{
if( m_alarms[ (i + st.wDayOfWeek) % 7 ].empty() )
Expand Down Expand Up @@ -523,14 +552,18 @@ void CMainFrame::SetToolTip()
found = true;
break;
}
m_snoozeTimeMutex.Unlock();
m_alarmsMutex.Unlock();
if( !found )
_stprintf( buf, _T("iSnooze\nNo alarms have any days selected!") );
}

}
else
_tcscpy( buf, _T("iSnooze\nAlarm disabled") );
m_systrayMutex.Lock();
m_systray.SetTooltipText( buf );
m_systrayMutex.Unlock();
}

// CMainFrame diagnostics
Expand Down Expand Up @@ -653,6 +686,7 @@ void CMainFrame::OnTestLaunch()

void CMainFrame::OnAlarmEnabled()
{
m_systrayMutex.Lock();
if( m_systray.GetMenuItemChecked( ID_APP_ALARMENABLED ) == MF_CHECKED )
{
if( m_snoozing )
Expand All @@ -672,6 +706,7 @@ void CMainFrame::OnAlarmEnabled()
//m_systray.SetIcon( IDR_MAINFRAME );
SetIcon();
}
m_systrayMutex.Unlock();
SetToolTip();
}

Expand Down
29 changes: 16 additions & 13 deletions MainFrm.h
Expand Up @@ -81,6 +81,7 @@ class CMainFrame : public CFrameWnd
// Generated message map functions
protected:
CSystemTray m_systray;
CMutex m_systrayMutex;
CConfigDlg *m_config;
RegMap m_reg;

Expand All @@ -90,27 +91,29 @@ class CMainFrame : public CFrameWnd
long m_tminute;*/
//DayTime::TimeAndDays m_alarmTime;
DayTime::TimeAndDays m_snoozeAlarmTime;
bool m_snoozing;
CMutex m_snoozeTimeMutex;
volatile bool m_snoozing;
ITID m_pls;
tstring m_plsname;
bool m_shuffle;
bool m_increase;
bool m_minimize;
bool m_enableSnooze;
unsigned char m_snoozeTime;
int m_inclength;
VolumeIncreaseState m_volumeIncreasing;
bool m_muteOnRet;
bool m_snoozeDlgOpen;
volatile bool m_shuffle;
volatile bool m_increase;
volatile bool m_minimize;
volatile bool m_enableSnooze;
volatile unsigned char m_snoozeTime;
volatile int m_inclength;
volatile VolumeIncreaseState m_volumeIncreasing;
volatile bool m_muteOnRet;
volatile bool m_snoozeDlgOpen;

bool m_alarmEnabled;
volatile bool m_alarmEnabled;

bool m_oneAlarmExists;
bool m_oneAlarmEnabled;
volatile bool m_oneAlarmExists;
volatile bool m_oneAlarmEnabled;

DWORD m_mainThread;

std::vector< std::list<DayTime::TimeAndDays*> > m_alarms;
CMutex m_alarmsMutex;

void LoadReg();
void InitReg();
Expand Down
1 change: 1 addition & 0 deletions stdafx.h
Expand Up @@ -53,6 +53,7 @@
#include <afxext.h> // MFC extensions
#include <afxtempl.h> // MFC collection classes
#include <afxdisp.h> // MFC Automation classes
#include <afxmt.h> // MFC Multithreading classes

#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
Expand Down

0 comments on commit 22f64af

Please sign in to comment.