File: AsyncSync.cpp

package info (click to toggle)
kwave 0.7.2-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,048 kB
  • ctags: 4,906
  • sloc: cpp: 31,275; ansic: 13,111; sh: 9,511; perl: 2,724; makefile: 786; asm: 145
file content (100 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************************************************
			  AsyncSync.cpp  -  multithreading support for qt
			     -------------------
    begin                : Jun 1997
    copyright            : (C) 2000 by Brian Theodore,
                           Science Applications International Corp.
                           Simulation Technology Division
    email                : theodore@std.saic.com

  (copied to the kwave project by Thomas.Eschenbacher@gmx.de at Jun 2000)
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <unistd.h> // for pipe
#include <stdio.h>

#include <qobject.h>
#include <qsocketnotifier.h>

#include "mt/Mutex.h"
#include "mt/MutexGuard.h"
#include "mt/AsyncSync.h"

/** global lock for creating/deleting socket notifiers */
Mutex AsyncSync::m_lock;

//*****************************************************************************
AsyncSync::AsyncSync()
    :QObject()
{
    MutexGuard lock(m_lock);

    // Create IPC pipe for async/sync communication with X server
    if ( ::pipe(m_fds) == -1 ) {
	perror( "Creating pipe" );
	m_fds[0] = m_fds[1] = -1;
    }

    // Create new socket notifier to monitor when data is ready to be
    // read from pipe
    m_sn = new QSocketNotifier(m_fds[0], QSocketNotifier::Read);
    Q_ASSERT(m_sn);

    // Connect up the socket notifier's activated routine to dequeue
    // any new clients added to Database
    if (m_sn) connect(m_sn, SIGNAL(activated(int)),
                      this, SLOT(SyncHandler()) );
}

//*****************************************************************************
AsyncSync::~AsyncSync()
{
    MutexGuard lock(m_lock);

    // Delete socket notifier
    if (m_sn) delete m_sn;

    // Close pipe file descriptors
    if ( ::close(m_fds[0] ) == -1 ) {
	perror( "Closing read file descriptor" );
    }

    if ( ::close(m_fds[1] ) == -1 ) {
	perror( "Closing writing file descriptor" );
    }
}

//*****************************************************************************
void AsyncSync::SyncHandler()
{
    // First remove message from pipe ( the writer only wrote 1 byte )
    static char dummy;
    if ( ::read( m_fds[0], &dummy, 1 ) == -1 ) {
	::perror( "Reading from pipe" );
    }

    // Now emit activated signal, and let user decide what to do
    emit Activated();
}

//*****************************************************************************
void AsyncSync::AsyncHandler()
{
    // Just send a single byte of data;
    static const char dummy = 0x00;
    if ( ::write( m_fds[1], &dummy, 1 ) == -1 ) {
	::perror( "Writing to pipe" );
    }
}

//*****************************************************************************
//*****************************************************************************