mxnet
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
dmlc::LuaState Class Reference

A Lua state. More...

#include <lua.h>

Collaboration diagram for dmlc::LuaState:
Collaboration graph

Public Types

enum  Option { kNoThreadProtect, kThreadLocal, kLocking }
 options to be provided in lua state More...
 

Public Member Functions

 ~LuaState ()
 destructor More...
 
LuaRef Eval (const char *lua_code)
 evaluate a piece of lua code, return the first result. More...
 
LuaRef Eval (const std::string &lua_code)
 evaluate a piece of lua code, return the first result. More...
 
template<typename T >
LuaRef Convert (const T &value)
 convert a C++ type to lua type More...
 
LuaRef operator[] (const std::string &key)
 get global field from the state More...
 
void SetGlobalField (const std::string &key, const LuaRef &value)
 Set the value to the global table. More...
 
template<typename F >
void PRun_ (F f)
 protected run f, this is used by API developers. always call this to access lua state f must not destruct LuaRef, or access the mutex More...
 
bool SameLuaState (lua_State *L) const
 

Static Public Member Functions

static LuaStateThreadLocalState ()
 
static LuaStateCreate_ (Option option)
 

Protected Member Functions

 LuaState ()
 constructor More...
 

Protected Attributes

Option option_ {kThreadLocal}
 internal option, default to thread local More...
 
lua_State * L_
 internal lua state More...
 
std::mutex mutex_
 internal lock about the state More...
 

Friends

class LuaRef
 
struct ThreadLocalStore< LuaState >
 

Detailed Description

A Lua state.

Member Enumeration Documentation

options to be provided in lua state

Enumerator
kNoThreadProtect 
kThreadLocal 
kLocking 

Constructor & Destructor Documentation

dmlc::LuaState::~LuaState ( )
inline

destructor

dmlc::LuaState::LuaState ( )
inlineprotected

constructor

Member Function Documentation

template<typename T >
LuaRef dmlc::LuaState::Convert ( const T &  value)
inline

convert a C++ type to lua type

Parameters
valueThe data to be converted. vector, map will be converted to table.
Returns
a converted value.
Template Parameters
Tthe type to be converted.
static LuaState* dmlc::LuaState::Create_ ( Option  option)
inlinestatic

Create a new lua state.

Note
It is highly recommended to use ThreadLocalState instead.

Most Lua program assumes it only runs from the same thread. Some Lua code that wraps C library(e.g. Torch) could rely on thread_local storage to store global state such as random number generator. This means if the code is invoked by another thread, the thread_local might become inavailable, depending on the implementation.

If the global state is stored only in Lua's global table, then it is safe to use kLocking mode and call the code from multiple thread. Never-the-less, using ThreadLocalState removes the need to lock, and is the desirable usecase in most times.

See also
ThreadLocalState
Parameters
optionThe option to use the state.
Returns
a newly created lua state
LuaRef dmlc::LuaState::Eval ( const char *  lua_code)
inline

evaluate a piece of lua code, return the first result.

Parameters
lua_codeLua code
Returns
A LuaRef object of the first returned result, Can be nil if the code did not return LuaRefthing.
LuaRef dmlc::LuaState::Eval ( const std::string &  lua_code)
inline

evaluate a piece of lua code, return the first result.

Parameters
lua_codeLua code
Returns
A LuaRef object of the first returned result, Can be nil if the code did not return anything.
LuaRef dmlc::LuaState::operator[] ( const std::string &  key)
inline

get global field from the state

Parameters
keyThe key to the global field.
Returns
The global field value.
template<typename F >
void dmlc::LuaState::PRun_ ( f)
inline

protected run f, this is used by API developers. always call this to access lua state f must not destruct LuaRef, or access the mutex

Parameters
fthe function to be called.
Template Parameters
Fthe function to be called, signiture (lua_State *L)
bool dmlc::LuaState::SameLuaState ( lua_State *  L) const
inline
Parameters
Lthe other lua state.
Returns
if the internal lua state is same as L
void dmlc::LuaState::SetGlobalField ( const std::string &  key,
const LuaRef value 
)
inline

Set the value to the global table.

Parameters
keyThe key of the global field.
valueThe value to the set.
static LuaState* dmlc::LuaState::ThreadLocalState ( )
inlinestatic

Get a thread local version of lua state. The LuaState runs in thread local mode, all the LuaRef can only be run on the current thread. This is the recommended behavior when invoking Lua.

Returns
a threadlocal version of lua state.

Friends And Related Function Documentation

friend class LuaRef
friend
friend struct ThreadLocalStore< LuaState >
friend

Member Data Documentation

lua_State* dmlc::LuaState::L_
protected

internal lua state

std::mutex dmlc::LuaState::mutex_
protected

internal lock about the state

Option dmlc::LuaState::option_ {kThreadLocal}
protected

internal option, default to thread local


The documentation for this class was generated from the following file: