Skip to content

Instantly share code, notes, and snippets.

View jsbattig's full-sized avatar

Jose Sebastian Battig jsbattig

View GitHub Profile
@jsbattig
jsbattig / java_keepalive.java
Created November 12, 2016 18:19
Java KeepAlive() function
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
public class NativeMember {
private static int staticCounter = 0;
private static final AtomicIntegerFieldUpdater<NativeMember> updater = AtomicIntegerFieldUpdater.newUpdater(NativeMember.class, "counter");
private volatile int counter = staticCounter;
public void keepAlive() {
updater.lazySet(this, counter + 1);
}
@jsbattig
jsbattig / OraDeltaResolution.cs
Created April 25, 2016 20:19
Issue trying to pull deltas in Oracle using date field
using System;
using System.Data;
using Oracle.DataAccess.Client;
namespace oradatetest
{
class Program
{
static void Main(string[] args)
{
@jsbattig
jsbattig / PointerTrie.pas
Last active December 24, 2015 03:43
Pointer-Trie implementation for fast insertion, finding, and removal of Pointers in a space-efficient manner (about 30% space efficiency in average)
unit PointerTrie;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
const
@jsbattig
jsbattig / cpp_objects_constructor_and_assignment_operator_calling.cpp
Created October 13, 2015 21:28
Example in C++ of constructor, copy constructor and assignment operator calling
#include <stdio.h>
#include <string>
using namespace std;
class myString {
static int globalCounter;
string str;
public:
int counter;
@jsbattig
jsbattig / BreakSwitchToFiber.pas
Created March 16, 2015 04:16
Sample test that will break fiber utilization in Delphi
procedure TestTJobQueue.JobMethodRaiseAndSwitchToFiber(const AJob: IJob; const AParams: array of Variant);
begin
try
Check(True);
raise ETestJobQueue.Create('Hello');
except
on E : Exception do
begin
try
raise ETestJobQueue.Create('Hello Nested');
@jsbattig
jsbattig / DelphiSwitchToFiber.pas
Last active July 28, 2023 18:55
Unit to properly SwitchToFiber() in Delphi without breaking exception handling
{
The idea of DelphiSwitchToFiber() function is to backup on a local variable in stack the
state of the Exception stack right before calling SwitchToFiber() and then restoring its state
right atfer returns from call to SwitchToFiber().
If SwitchToFiber() is used directly from within an Except or Finally block, and if there's an exception
raised after switching to another fiber, upon coming back the results will be unpredictable because
the exception stack will be completely unwinded and all raise exceptions destroyed.
In order to prevent this issue we must backup the Exception stack before the call to SwitchToFiber()
and restore it right after the call.
@jsbattig
jsbattig / uWin64ExceptionStack.pas
Created March 16, 2015 04:07
Delphi exception stack backup and restore class
unit uWin64ExceptionStack;
interface
{$IFDEF WIN64}
const
MAX_NESTED_EXCEPTIONS = 16;
type
@jsbattig
jsbattig / SvcBusThreadPoolTimer_init() fixed.c
Created March 6, 2014 00:04
SvcBusThreadPoolTimer_init() fixed
int SvcBusThreadPoolTimer_init( SvcBusThreadPoolTimer* _this, SvcBusThreadPool* pool, SvcBusThreadPool_callback callback, void* context, unsigned int millis )
{
FILETIME FileDueTime;
ULARGE_INTEGER ulDueTime;
_this->timer = CreateThreadpoolTimer( &SvcBusThreadPoolTimer_TimerCallback, _this, &pool->pcbe );
if( _this->timer == NULL )
{
return SVCBUS_THREADPOOL_ERROR;
}
@jsbattig
jsbattig / testThreadPoolTimer.c
Created March 5, 2014 23:57
ThreadPoolTimer test
TEST_F(SvcBusThreadPoolTest, SvcBusConsumer_testThreadPoolTimer) {
SvcBusThreadPoolTimer timer[1];
int int_value = 0;
ASSERT_TRUE(SvcBusThreadPoolTimer_init( timer, thread_pool, &TestCallback, &int_value, 100 ) == SVCBUS_THREADPOOL_OK);
EXPECT_EQ(0, SvcBusThreadPoolTimer_getThreadId( timer));
SvcBus_crossSleep(500);
EXPECT_TRUE( int_value > 0 );
EXPECT_NE(0, SvcBusThreadPoolTimer_getThreadId( timer));
SvcBusThreadPoolTimer_destroy( timer );
@jsbattig
jsbattig / SvcBusThreadPoolTimer_init().c
Last active August 29, 2015 13:57
SvcBusThreadPoolTimer constructor method (with bug)
int SvcBusThreadPoolTimer_init( SvcBusThreadPoolTimer* _this, SvcBusThreadPool* pool, SvcBusThreadPool_callback callback, void* context, unsigned int millis )
{
FILETIME FileDueTime;
ULARGE_INTEGER ulDueTime;
_this->timer = CreateThreadpoolTimer( &SvcBusThreadPoolTimer_TimerCallback, _this, &pool->pcbe );
if( _this->timer == NULL )
{
return SVCBUS_THREADPOOL_ERROR;
}