first commit
This commit is contained in:
67
include/platform/async/future.h
Normal file
67
include/platform/async/future.h
Normal file
@ -0,0 +1,67 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NFUTURE__
|
||||
#define __NFUTURE__
|
||||
|
||||
|
||||
#include "thread/atomic_value.h"
|
||||
#include "memory/nshared_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace ASync {
|
||||
|
||||
/*
|
||||
@short Future.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
template <class T> class Future
|
||||
{
|
||||
T value;
|
||||
Threading::Atomic32 set;
|
||||
|
||||
public:
|
||||
|
||||
bool IsSet() const
|
||||
{ return set.Get() == 1; }
|
||||
void Set(const T &_value)
|
||||
{
|
||||
value = _value;
|
||||
set.Set(1);
|
||||
}
|
||||
|
||||
void Wait()
|
||||
{ while (!IsSet()); }
|
||||
|
||||
T &Get()
|
||||
{
|
||||
Wait();
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
template <> class Future <void>
|
||||
{
|
||||
Threading::Atomic32 set;
|
||||
|
||||
public:
|
||||
|
||||
bool IsSet() const
|
||||
{ return set.Get() == 1; }
|
||||
void Set()
|
||||
{ set.Set(1); }
|
||||
|
||||
void Wait()
|
||||
{ while (!IsSet()); }
|
||||
};
|
||||
|
||||
} // ASync
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NFUTURE__
|
||||
Reference in New Issue
Block a user