Skip to content

Commit

Permalink
co
Browse files Browse the repository at this point in the history
  • Loading branch information
idealvin committed Nov 10, 2019
1 parent 5116454 commit aa97cc0
Show file tree
Hide file tree
Showing 184 changed files with 37,117 additions and 0 deletions.
30 changes: 30 additions & 0 deletions base/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os, sys, time
from glob import glob

env = Environment()

# cxxflags passed to g++ only
cxxflags = ['-std=c++11']
ccflags = ['-O2', '-g3', '-Wall']
ccdefines = {'_FILE_OFFSET_BITS' : '64', 'DEBUG' : 1, }

if sys.platform == 'darwin':
env['CXX'] = 'clang++'
env['CC'] = 'clang'
ccflags.append('-fno-pie')

env.Append(CPPFLAGS = ccflags)
env.Append(CXXFLAGS = cxxflags)
env.Append(CPPDEFINES = ccdefines)
env.Append(CPPPATH = ['/usr/local/include', '../'])

source_files = glob('../base/*.cc') + glob('../base/*/*.cc') + \
glob('../base/co/impl/*.cc')
source_files += ['../base/co/context/context.S']

print("souce code list: >>")
for s in source_files:
print(os.path.realpath(s))
print('')

env.Library('base', source_files)
1 change: 1 addition & 0 deletions base/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SConscript('SConscript', variant_dir='../lib', duplicate=0)
54 changes: 54 additions & 0 deletions base/_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash -e

cd "$(dirname "$0")"

if [[ ! -d "../lib" ]];then
mkdir ../lib
fi

platform=`uname -m`

if [[ "$platform" == "x86_64" ]]; then
PLAT="x64"
else
PLAT="x86"
fi

if [[ "$OSTYPE" == "linux-gnu" ]]; then
OS="linux"
CC="g++"
CCFLAGS="-std=c++11"
AR="ar"
OUT="libbase.a"
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="mac"
CC="clang++"
CCFLAGS="-std=c++11"
AR="ar"
OUT="libbase.a"
elif [[ "$OSTYPE" == "msys" ]]; then
OS="win"
CC="clang++"
CCFLAGS="-Xclang -flto-visibility-public-std -Wno-deprecated"
AR="llvm-ar"
OUT="base.lib"
else
OS="oth"
CC="g++"
CCFLAGS="-std=c++11"
AR="ar"
OUT="libbase.a"
fi

$CC $CCFLAGS -O2 -g3 -c *.cc */*.cc co/impl/*.cc co/context/context.S

if [[ ! -d "../lib/$OS/$PLAT" ]]; then
mkdir -p ../lib/$OS/$PLAT
fi

$AR rc ../lib/$OS/$PLAT/$OUT *.o
rm -rf *.o

echo lib/$OS/$PLAT/$OUT created ok.

exit 0
14 changes: 14 additions & 0 deletions base/_clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

if [[ "$OSTYPE" == "msys" ]]; then
echo "don't run this on windows.."
exit 0
fi

src=`ls *.{h,cc} */*.{h,cc,cpp} */*/*.{h,cc}`
for x in $src
do
if [ -f $x ]; then
sed -i 's/[ ]*$//g' $x
fi
done
7 changes: 7 additions & 0 deletions base/atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#ifdef _WIN32
#include "win/atomic.h"
#else
#include "unix/atomic.h"
#endif
76 changes: 76 additions & 0 deletions base/byte_order.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#ifdef _WIN32
#include <WinSock2.h>
#pragma comment(lib,"WS2_32.lib")


inline unsigned short hton16(unsigned short v) {
return htons(v);
}

inline unsigned int hton32(unsigned int v) {
return htonl(v);
}

inline unsigned long long hton64(unsigned long long v) {
return htonll(v);
}

inline unsigned short ntoh16(unsigned short v) {
return ntohs(v);
}

inline unsigned int ntoh32(unsigned int v) {
return ntohl(v);
}

inline unsigned long long ntoh64(unsigned long long v) {
return ntohll(v);
}

#else

#include <stdint.h>

#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>

#define htobe16 OSSwapHostToBigInt16
#define htobe32 OSSwapHostToBigInt32
#define htobe64 OSSwapHostToBigInt64
#define be16toh OSSwapBigToHostInt16
#define be32toh OSSwapBigToHostInt32
#define be64toh OSSwapBigToHostInt64

#elif defined(__linux__)
#include <endian.h>
#else
#include <sys/endian.h>
#endif

inline uint16_t hton16(uint16_t v) {
return htobe16(v);
}

inline uint32_t hton32(uint32_t v) {
return htobe32(v);
}

inline uint64_t hton64(uint64_t v) {
return htobe64(v);
}

inline uint16_t ntoh16(uint16_t v) {
return be16toh(v);
}

inline uint32_t ntoh32(uint32_t v) {
return be32toh(v);
}

inline uint64_t ntoh64(uint64_t v) {
return be64toh(v);
}

#endif
128 changes: 128 additions & 0 deletions base/closure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#pragma once

#include "def.h"
#include <functional>
#include <unordered_map>

class Closure {
public:
Closure() = default;

virtual void run() = 0;

protected:
virtual ~Closure() {}
};

namespace xx {

class Function0 : public Closure {
public:
typedef void (*F)();

Function0(F f)
: _f(f) {
}

virtual ~Function0() {}

virtual void run() {
_f();
}

private:
F _f;
};

class Function1 : public Closure {
public:
typedef void (*F)(void*);

Function1(F f, void* p)
: _f(f), _p(p) {
}

virtual void run() {
_f(_p);
delete this;
}

private:
F _f;
void* _p;

virtual ~Function1() {}
};

template<typename T>
class Method0 : public Closure {
public:
typedef void (T::*F)();

Method0(F f, T* p)
: _f(f), _p(p) {
}

virtual void run() {
(_p->*_f)();
delete this;
}

private:
F _f;
T* _p;

virtual ~Method0() {}
};

class Function : public Closure {
public:
typedef std::function<void()> F;

Function(F&& f)
: _f(std::move(f)) {
}

Function(const F& f)
: _f(f) {
}

virtual void run() {
_f();
delete this;
}

private:
F _f;

virtual ~Function() {}
};

} // xx

inline Closure* new_callback(void (*f)()) {
typedef std::unordered_map<void(*)(), xx::Function0> Map;
static __thread Map* kF0 = 0;
if (unlikely(!kF0)) kF0 = new Map();

auto it = kF0->find(f);
if (it != kF0->end()) return &it->second;
return &kF0->insert(std::make_pair(f, xx::Function0(f))).first->second;
}

inline Closure* new_callback(void (*f)(void*), void* p) {
return new xx::Function1(f, p);
}

template<typename T>
inline Closure* new_callback(void (T::*f)(), T* p) {
return new xx::Method0<T>(f, p);
}

inline Closure* new_callback(std::function<void()>&& f) {
return new xx::Function(std::move(f));
}

inline Closure* new_callback(const std::function<void()>& f) {
return new xx::Function(f);
}
3 changes: 3 additions & 0 deletions base/co.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include "co/co.h"
Loading

0 comments on commit aa97cc0

Please sign in to comment.