Click here to Skip to main content
15,895,777 members
Articles / Programming Languages / C

A Generic C-Language TCP Client Application

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
9 May 2010CPOL4 min read 29.1K   793   20  
A library for writing simple TCP client applications
/// @source      Mutex.c
/// @description Implementation of Mutex utility.
//  See licensing information in the file README.TXT.

// -----------------------------------------------------------------------------

// includes

// common configuration options & declarations
#include "../config.h"  // always include first

// C language includes
#include <assert.h>
#include <stdlib.h> // calloc

#if PLATFORM(Windows)
#include <windows.h>
#endif

#if PLATFORM(Linux)
#include <pthread.h>
#endif

// framework support
#include "Mutex.h" /* mutex_xxx functions */

// -----------------------------------------------------------------------------

// global declarations

/// Mutex class.
/// This class that encapsulates a platform-specific mutex facility.
/// @class Mutex

// Linux-specific declarations
#if PLATFORM(Linux)
struct mutex_t
{
   /// Native Linux mutex handle.
   pthread_mutex_t handle;
};
#endif

// Windows-specific declarations
#if PLATFORM(Windows)
struct mutex_t
{
   /// Native Windows mutex handle.
   HANDLE handle;
};
#endif

// -----------------------------------------------------------------------------
// PUBLIC INTERFACE
// -----------------------------------------------------------------------------

/** Creates a Mutex instance.
    @fn mutex_t* mutex_create(void)

    @return
    address of Mutex created

    @post
    Either succeeds or aborts application.

    @memberof Mutex
*/

#if PLATFORM(Linux)
mutex_t* mutex_create(void)
{
   int result;
   mutex_t* mutex = (mutex_t*)calloc(1, sizeof(mutex_t));

   // memory allocation successful ?
   assert(mutex);

   // initializes mutex
   result = pthread_mutex_init(&mutex->handle, NULL);
   assert(result == 0);

   return mutex;
}
#endif

#if PLATFORM(Windows)
TC2API mutex_t* mutex_create(void)
{
   mutex_t* mutex = (mutex_t*)calloc(1, sizeof(mutex_t));

   // memory allocation successful ?
   assert(mutex);

   mutex->handle = CreateMutex(
      NULL,  // LPSECURITY_ATTRIBUTES
      FALSE, // initial owner
      NULL   // name
      );

   assert(mutex->handle);

   return mutex;
}
#endif

// -----------------------------------------------------------------------------
// PUBLIC INTERFACE
// -----------------------------------------------------------------------------

/** Locks a Mutex.
    @fn void mutex_lock(mutex_t* mutex)

    @param [in]
    mutex : address of mutex to be locked.

    @post
    Either succeeds or aborts application.

    @memberof Mutex
*/

#if PLATFORM(Linux)
void mutex_lock(const mutex_t* mutex)
{
   int result = pthread_mutex_lock(&(((mutex_t*)mutex)->handle));
   assert(result == 0);
}
#endif

#if PLATFORM(Windows)
TC2API void mutex_lock(const mutex_t* mutex)
{
   DWORD result = WaitForSingleObject(mutex->handle, INFINITE);
   assert(result == WAIT_OBJECT_0);
}
#endif

// -----------------------------------------------------------------------------
// PUBLIC INTERFACE
// -----------------------------------------------------------------------------

/** Unlocks a mutex.
    @fn void mutex_unlock(mutex_t* mutex)

    @param [in]
    mutex : address of mutex to be unlocked.

    @post
    Either succeeds or aborts application.

    @memberof Mutex
*/

#if PLATFORM(Linux)
void mutex_unlock(const mutex_t* mutex)
{
   int result = pthread_mutex_unlock(&(((mutex_t*)mutex)->handle));
   assert(result == 0);
}
#endif

#if PLATFORM(Windows)
TC2API void mutex_unlock(const mutex_t* mutex)
{
   ReleaseMutex(mutex->handle);
}
#endif

// -----------------------------------------------------------------------------
// the end

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
zvx
Software Developer
Brazil Brazil
I'm a long-time software developer living in Brazil.

I've been developing software for retail and banking automation in C/C++ for many years now. In the old days I even did some COBOL programming, and some assembly for the 8080.

My experience ranges from low level software such as interface code for serial devices for DOS and Windows (bar code scanners, printers, cash dispensers, etc) and goes to writing end user applications for POS terminals and bank ATMs. In between I've done a great deal of TCP/IP programming using the basic Berkeley sockets interface, which is my main interest nowadays.

Comments and Discussions