text:
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
using X509IssuerSerial = System.Security.Cryptography.Xml.X509IssuerSerial;

using Microsoft.Win32.SafeHandles;

using Internal.NativeCrypto;

using static Interop.Crypt32;

namespace Internal.Cryptography.Pal.Windows
{
    internal static class HelpersWindows
    {
        public static CryptographicException ToCryptographicException(this ErrorCode errorCode)
        {
            return ((int)errorCode).ToCryptographicException();
        }

        public static string ToStringAnsi(this IntPtr psz)
        {
            return Marshal.PtrToStringAnsi(psz);
        }

        // Used for binary blobs without internal pointers.
        public static byte[] GetMsgParamAsByteArray(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0)
        {
            int cbData = 0;
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, null, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            byte[] pvData = new byte[cbData];
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, pvData, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            return pvData.Resize(cbData);
        }

        // Used for binary blobs with internal pointers.
        public static SafeHandle GetMsgParamAsMemory(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0)
        {
            int cbData = 0;
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, null, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            SafeHandle pvData = SafeHeapAllocHandle.Alloc(cbData);
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, pvData.DangerousGetHandle(), ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            return pvData;
        }

        public static byte[] ToByteArray(this DATA_BLOB blob)
        {
            if (blob.cbData == 0)
                return Array.Empty<byte>();

            int length = (int)(blob.cbData);
            byte[] data = new byte[length];
            Marshal.Copy(blob.pbData, data, 0, length);
            return data;
        }

        public static CryptMsgType GetMessageType(this SafeCryptMsgHandle hCryptMsg)
        {
            int cbData = sizeof(CryptMsgType);
            CryptMsgType cryptMsgType;
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_TYPE_PARAM, 0, out cryptMsgType, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            return cryptMsgType;
        }

        public static int GetVersion(this SafeCryptMsgHandle hCryptMsg)
        {
            int cbData = sizeof(int);
            int version;
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_VERSION_PARAM, 0, out version, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            return version;
        }

        /// <summary>
        /// Returns the inner content of the CMS.
        ///
        /// Special case: If the CMS is an enveloped CMS that has been decrypted and the inner content type is Oids.Pkcs7Data, the returned
        /// content bytes are the decoded octet bytes, rather than the encoding of those bytes. This is a documented convenience behavior of
        /// CryptMsgGetParam(CMSG_CONTENT_PARAM) that apparently got baked into the behavior of the managed EnvelopedCms class.
        /// </summary>
        public static ContentInfo GetContentInfo(this SafeCryptMsgHandle hCryptMsg)
        {
            byte[] oidBytes = hCryptMsg.GetMsgParamAsByteArray(CryptMsgParamType.CMSG_INNER_CONTENT_TYPE_PARAM);

            // Desktop compat: If we get a null or non-terminated string back from Crypt32, throwing an exception seems more apropros but
            // for the desktop compat, we throw the result at the ASCII Encoder and let the chips fall where they may.
            int length = oidBytes.Length;
            if (length > 0 && oidBytes[length - 1] == 0)
            {
                length--;
            }

            string oidValue = Encoding.ASCII.GetString(oidBytes, 0, length);
            Oid contentType = new Oid(oidValue);
            byte[] content = hCryptMsg.GetMsgParamAsByteArray(CryptMsgParamType.CMSG_CONTENT_PARAM);

            return new ContentInfo(contentType, content);
        }

        public static X509Certificate2Collection GetOriginatorCerts(this SafeCryptMsgHandle hCryptMsg)
        {
            int numCertificates = 0;
            int cbNumCertificates = sizeof(int);
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_CERT_COUNT_PARAM, 0, out numCertificates, ref cbNumCertificates))
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            X509Certificate2Collection certs = new X509Certificate2Collection();
            for (int index = 0; index < numCertificates; index++)
            {
                byte[] encodedCertificate = hCryptMsg.GetMsgParamAsByteArray(CryptMsgParamType.CMSG_CERT_PARAM, index);
                X509Certificate2 cert = new X509Certificate2(encodedCertificate);
                certs.Add(cert);
            }
            return certs;
        }

        /// <summary>
        /// Returns (AlgId)(-1) if oid is unknown.
        /// </summary>
        public static AlgId ToAlgId(this string oidValue)
        {
            CRYPT_OID_INFO info = OidInfo.FindOidInfo(CryptOidInfoKeyType.CRYPT_OID_INFO_OID_KEY, oidValue, OidGroup.All, false);
            return (AlgId)(info.AlgId);
        }

        public static SafeCertContextHandle CreateCertContextHandle(this X509Certificate2 cert)
        {
            IntPtr pCertContext = cert.Handle;
            pCertContext = Interop.Crypt32.CertDuplicateCertificateContext(pCertContext);
            SafeCertContextHandle hCertContext = new SafeCertContextHandle(pCertContext);
            GC.KeepAlive(cert);
            return hCertContext;
        }

        public unsafe static byte[] GetSubjectKeyIdentifer(this SafeCertContextHandle hCertContext)
        {
            int cbData = 0;
            if (!Interop.Crypt32.CertGetCertificateContextProperty(hCertContext, CertContextPropId.CERT_KEY_IDENTIFIER_PROP_ID, null, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            byte[] ski = new byte[cbData];
            if (!Interop.Crypt32.CertGetCertificateContextProperty(hCertContext, CertContextPropId.CERT_KEY_IDENTIFIER_PROP_ID, ski, ref cbData))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            return ski.Resize(cbData);
        }

        public static SubjectIdentifier ToSubjectIdentifier(this CERT_ID certId)
        {
            switch (certId.dwIdChoice)
            {
                case CertIdChoice.CERT_ID_ISSUER_SERIAL_NUMBER:
                    {
                        const CertNameStrTypeAndFlags dwStrType = CertNameStrTypeAndFlags.CERT_X500_NAME_STR | CertNameStrTypeAndFlags.CERT_NAME_STR_REVERSE_FLAG;
                        string issuer = Interop.Crypt32.CertNameToStr(ref certId.u.IssuerSerialNumber.Issuer, dwStrType);
                        byte[] serial = certId.u.IssuerSerialNumber.SerialNumber.ToByteArray();
                        X509IssuerSerial issuerSerial = new X509IssuerSerial(issuer, serial.ToSerialString());
                        return new SubjectIdentifier(SubjectIdentifierType.IssuerAndSerialNumber, issuerSerial);
                    }

                case CertIdChoice.CERT_ID_KEY_IDENTIFIER:
                    {
                        byte[] ski = certId.u.KeyId.ToByteArray();
                        return new SubjectIdentifier(SubjectIdentifierType.SubjectKeyIdentifier, ski.ToSkiString());
                    }

                default:
                    throw new CryptographicException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, certId.dwIdChoice));
            }
        }

        public static SubjectIdentifierOrKey ToSubjectIdentifierOrKey(this CERT_ID certId)
        {
            //
            // SubjectIdentifierOrKey is just a SubjectIdentifier with an (irrelevant here) "key" option thumbtacked onto it so
            // the easiest way is to subcontract the job to SubjectIdentifier.
            //
            SubjectIdentifier subjectIdentifier = certId.ToSubjectIdentifier();
            SubjectIdentifierType subjectIdentifierType = subjectIdentifier.Type;
            switch (subjectIdentifierType)
            {
                case SubjectIdentifierType.IssuerAndSerialNumber:
                    return new SubjectIdentifierOrKey(SubjectIdentifierOrKeyType.IssuerAndSerialNumber, subjectIdentifier.Value);

                case SubjectIdentifierType.SubjectKeyIdentifier:
                    return new SubjectIdentifierOrKey(SubjectIdentifierOrKeyType.SubjectKeyIdentifier, subjectIdentifier.Value);

                default:
                    Debug.Assert(false);  // Only the framework can construct SubjectIdentifier's so if we got a bad value here, that's our fault.
                    throw new CryptographicException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, subjectIdentifierType));
            }
        }

        public static SubjectIdentifierOrKey ToSubjectIdentifierOrKey(this CERT_PUBLIC_KEY_INFO publicKeyInfo)
        {
            int keyLength = Interop.Crypt32.CertGetPublicKeyLength(MsgEncodingType.All, ref publicKeyInfo);
            string oidValue = publicKeyInfo.Algorithm.pszObjId.ToStringAnsi();
            AlgorithmIdentifier algorithmId = new AlgorithmIdentifier(Oid.FromOidValue(oidValue, OidGroup.PublicKeyAlgorithm), keyLength);

            byte[] keyValue = publicKeyInfo.PublicKey.ToByteArray();
            PublicKeyInfo pki = new PublicKeyInfo(algorithmId, keyValue);
            return new SubjectIdentifierOrKey(SubjectIdentifierOrKeyType.PublicKeyInfo, pki);
        }

        public static AlgorithmIdentifier ToAlgorithmIdentifier(this CRYPT_ALGORITHM_IDENTIFIER cryptAlgorithmIdentifer)
        {
            string oidValue = cryptAlgorithmIdentifer.pszObjId.ToStringAnsi();
            AlgId algId = oidValue.ToAlgId();

            int keyLength;
            switch (algId)
            {
                case AlgId.CALG_RC2:
                    {
                        if (cryptAlgorithmIdentifer.Parameters.cbData == 0)
                        {
                            keyLength = 0;
                        }
                        else
                        {
                            CRYPT_RC2_CBC_PARAMETERS rc2Parameters;
                            unsafe
                            {
                                int cbSize = sizeof(CRYPT_RC2_CBC_PARAMETERS);
                                if (!Interop.Crypt32.CryptDecodeObject(CryptDecodeObjectStructType.PKCS_RC2_CBC_PARAMETERS, cryptAlgorithmIdentifer.Parameters.pbData, (int)(cryptAlgorithmIdentifer.Parameters.cbData), &rc2Parameters, ref cbSize))
                                    throw Marshal.GetLastWin32Error().ToCryptographicException();
                            }

                            switch (rc2Parameters.dwVersion)
                            {
                                case CryptRc2Version.CRYPT_RC2_40BIT_VERSION:
                                    keyLength = KeyLengths.Rc2_40Bit;
                                    break;
                                case CryptRc2Version.CRYPT_RC2_56BIT_VERSION:
                                    keyLength = KeyLengths.Rc2_56Bit;
                                    break;
                                case CryptRc2Version.CRYPT_RC2_64BIT_VERSION:
                                    keyLength = KeyLengths.Rc2_64Bit;
                                    break;
                                case CryptRc2Version.CRYPT_RC2_128BIT_VERSION:
                                    keyLength = KeyLengths.Rc2_128Bit;
                                    break;
                                default:
                                    keyLength = 0;
                                    break;
                            }
                        }
                        break;
                    }

                case AlgId.CALG_RC4:
                    {
                        int saltLength = 0;
                        if (cryptAlgorithmIdentifer.Parameters.cbData != 0)
                        {
                            using (SafeHandle sh = Interop.Crypt32.CryptDecodeObjectToMemory(CryptDecodeObjectStructType.X509_OCTET_STRING, cryptAlgorithmIdentifer.Parameters.pbData, (int)cryptAlgorithmIdentifer.Parameters.cbData))
                            {
                                unsafe
                                {
                                    DATA_BLOB* pDataBlob = (DATA_BLOB*)(sh.DangerousGetHandle());
                                    saltLength = (int)(pDataBlob->cbData);
                                }
                            }
                        }

                        // For RC4, keyLength = 128 - (salt length * 8).
                        keyLength = KeyLengths.Rc4Max_128Bit - saltLength * 8;
                        break;
                    }

                case AlgId.CALG_DES:
                    // DES key length is fixed at 64 (or 56 without the parity bits).
                    keyLength = KeyLengths.Des_64Bit;
                    break;

                case AlgId.CALG_3DES:
                    // 3DES key length is fixed at 192 (or 168 without the parity bits).
                    keyLength = KeyLengths.TripleDes_192Bit;
                    break;

                default:
                    // We've exhausted all the algorithm types that the desktop used to set the KeyLength for. Key lengths are not a viable way of
                    // identifying algorithms in the long run so we will not extend this list any further.
                    keyLength = 0;
                    break;
            }

            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Oid.FromOidValue(oidValue, OidGroup.All), keyLength);
            return algorithmIdentifier;
        }

        public static CryptographicAttributeObjectCollection GetUnprotectedAttributes(this SafeCryptMsgHandle hCryptMsg)
        {
            // For some reason, you can't ask how many attributes there are - you have to ask for the attributes and
            // get a CRYPT_E_ATTRIBUTES_MISSING failure if the count is 0.
            int cbUnprotectedAttr = 0;
            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_UNPROTECTED_ATTR_PARAM, 0, null, ref cbUnprotectedAttr))
            {
                int lastError = Marshal.GetLastWin32Error();
                if (lastError == (int)ErrorCode.CRYPT_E_ATTRIBUTES_MISSING)
                    return new CryptographicAttributeObjectCollection();
                throw lastError.ToCryptographicException();
            }

            using (SafeHandle sh = hCryptMsg.GetMsgParamAsMemory(CryptMsgParamType.CMSG_UNPROTECTED_ATTR_PARAM))
            {
                unsafe
                {
                    CRYPT_ATTRIBUTES* pCryptAttributes = (CRYPT_ATTRIBUTES*)(sh.DangerousGetHandle());
                    return ToCryptographicAttributeObjectCollection(pCryptAttributes);
                }
            }
        }

        private static unsafe CryptographicAttributeObjectCollection ToCryptographicAttributeObjectCollection(CRYPT_ATTRIBUTES* pCryptAttributes)
        {
            CryptographicAttributeObjectCollection collection = new CryptographicAttributeObjectCollection();
            for (int i = 0; i < pCryptAttributes->cAttr; i++)
            {
                CRYPT_ATTRIBUTE* pCryptAttribute = &(pCryptAttributes->rgAttr[i]);
                AddCryptAttribute(collection, pCryptAttribute);
            }
            return collection;
        }

        private static unsafe void AddCryptAttribute(CryptographicAttributeObjectCollection collection, CRYPT_ATTRIBUTE* pCryptAttribute)
        {
            string oidValue = pCryptAttribute->pszObjId.ToStringAnsi();
            Oid oid = new Oid(oidValue);

            for (int i = 0; i < pCryptAttribute->cValue; i++)
            {
                byte[] encodedAttribute = pCryptAttribute->rgValue[i].ToByteArray();
                AsnEncodedData attributeObject = Helpers.CreateBestPkcs9AttributeObjectAvailable(oid, encodedAttribute);
                collection.Add(attributeObject);
            }
        }
    }
}


text:
{# TODO: wrap all these in their own divs for easier styling #}

{% if theme_donate_url or theme_opencollective or theme_tidelift_url %}

Donate/support

{% endif %}

{% if theme_donate_url %}

{% endif %}

{% if theme_opencollective %}

{% endif %}

{% if theme_tidelift_url %}

Professionally-supported {{ project }} is available with the
Tidelift Subscription.

{% endif %}

redpajama_set_name: RedPajamaGithub


text:
title: Just-In-Time (JIT) Compilation
author: vivekragunathan
layout: page
date: 2014-10-01T04:27:30+00:00

---
> An excerpt from the book Professional .NET v2.0

* * *

The Just in Time (JIT) Compiler gets its name because it compiles your managed code into executable native code on the fly. That is, it compiles the IL just as it’s needed. Understanding how the JIT works, again, isn’t strictly necessary in order to be productive with managed code. In fact, those not familiar with assembly-level programming might feel a little uncomfortable with the content in this section; if you’ve never programmed in a language like C or C++, for example, then you are sure to feel a little lost. Don’t fret; you can safely skim over this section and come back after you’ve worked with the CLR a little more. But for those who read through, it will give you some internals-style insight into how things work in the runtime.

### Compilation Process Overview

By default, the method table for each CTS type contains a slot for each method the type defines, including methods it has inherited from base classes. A “slot” is just a pointer to the memory address holding that method’s code; the collection of slots for a single type is called its _vtable_(a.k.a. vtbl, virtual table). This is much like the vtable C++ instances refer to. An object on the heap (to which a reference points) uses the first DWORD to point at that object’s method table. Following that pointer is the object’s runtime field data. This, among the overall JIT compilation process (explained below), is illustrated in Figure 3-6.

JIT compilation overview

All vtable slots contain an instruction (either a call or a jmp, we’ll see the difference in a moment) and a pointer to the IL for that slot. Initially, the instruction in each slot calls a shared piece of code, called the _pre-JIT stub_. This stub is responsible for invoking the JIT Compiler (which resides inmscorjit.dll) to generate native code for the method being called, back-patching the method-table slot with a jmp to the new location of the dynamically allocated code, and finishing with a jmp to that code. Thus, all slots that have already been jitted simply contain an unconditional jmp to the target jitted code in the instruction part of their slot. Having a jmp in the slot enables fast execution of calls with the overhead of only a single jmp instruction.

The JIT Compiler of course makes a number of intelligent operations while it is producing your code. For example, it will inline methods that are sufficiently small and don’t involve complex loops. It also performs many traditional compiler optimizations such as loop unrolling and dead code elimination. Code that is never exercised will not be jitted, and thus it won’t impact your working set. And of course any machine-specific features such as extended registers and preferences for instruction layout can be made, since compilation happens on the actual machine itself.

Unfortunately, the CLR JIT has to lose some intelligence in favor of code generation speed. Remember, it’s actually compiling your code as your application runs, so producing code that_executes fast_ isn’t always as important as _producing code fast_ that executes. A utility called NGen enables you to precompile managed code into native images, which doesn’t have this restriction. (Although, at the moment NGen doesn’t make any additional optimizations over what the ordinary JIT does. But of course the CLR Team is free to make these changes in the future — they are implementation details.) We discuss NGen further in Chapter 4.

### Method Call Internals

When a method call is made, the caller and callee methods must communicate a set of information with each other. We call the abstraction that contains this information an _activation frame_. The caller supplies the this pointer for instance methods, additional arguments for the method, and stack address information, while the receiver must give back the return value of the method and ensures that the stack has been cleaned up. All of this requires that a standard method-calling process be in place. This is referred to as a calling convention, of which there are several options on Windows.

Activation frames are implemented using a combination of registers and the physical OS stack, and are managed by the CLR’s JIT Compiler. There isn’t a “single activation frame object”; as noted above it’s a convention followed by the caller and callee. In addition to that, the CLR manages its own stack of frames to mark transitions in the stack, for example unmanaged to native calls, security asserts, and uses the information to mark the addresses of GC roots that are active in the call stack. These are stored in the TEB.

There are a number of ways to make method calls on the CLR. From entirely static to entirely dynamic (e.g. call, callvirt, calli, delegates), and everywhere in between, we’ll take a look at those in this section. The primary difference between the various method calls is the mechanism used to find the target address to which the generated native code must call.

We’ll use this set of types in our examples below:

[code lang=csharp]

using System;

using System.Runtime.CompilerServices;

class Foo

{

[MethodImpl(MethodImplOptions.NoInlining)]

public int f(string s, int x, int y)

{

Console.WriteLine("Foo::f({0},{1},{2})", s, x, y);

return x*y;

}

[MethodImpl(MethodImplOptions.NoInlining)]

public virtual int g(string s, int x, int y)

{

Console.WriteLine("Foo::g({0},{1},{2})", s, x, y);

return x+y;

}

}

class Bar : Foo

{

[MethodImpl(MethodImplOptions.NoInlining)]

public override int g(string s, int x, int y)

{

Console.WriteLine("Bar::g({0},{1},{2})", s, x, y);

return x-y;

}

}

delegate int Baz(string s, int x, int y);

[/code]

Furthermore, we’ll imagine the following variables are in scope for examples below:

[code lang=csharp]

Foo f = new Foo();

Bar b = new Bar();

[/code]

### A Word on the fastcall Calling Convention

The CLR’s jitted code uses the fastcall Windows calling convention. This permits the caller to supply the first two arguments (including this in the case of instance methods) in the machine’s ECX andEDX registers. Registers are significantly faster than using the machine’s stack, which is where the remaining arguments are supplied, in right-to-left order (using the push instruction).

### Ordinary Calls (call)

You might have already guessed the primary native code difference between an ordinary call and a virtual call based on the description above. Simply put, a virtual call looks at the method-table of the object against which the method is dispatching to determine the method-table slot to use for thecall, while others just use the token supplied at the call-site to determine statically which method-table slot to inspect. Slot offsets for both styles of calls are determined statically at JIT time, so they are quite fast; virtual methods ensure that their versions of methods inherited from base classes occupy the same slots so that the index for a particular method doesn’t depend on type.

Normal method calls (i.e., the IL call instruction, or callvirts to nonvirtual methods) are very fast. The JIT Compiler is able to burn the precise address of the target method-table slot at the call-site because it knows the location statically at compile time. Let’s consider an example:

[code lang=csharp]

int ff = f.f("Hi", 10, 10);

int bf = b.f("Hi", 10, 10);

[/code]

In this case, we’re calling the method f as defined on Foo. Although we use the b variable in the second line to make the call, f is nonvirtual and thus the call always goes through Foo’s definition. The jitted native code for both (in this example, IA-32 code) will be nearly identical:

[code lang=csharp]

mov ecx,esi

mov edx,dword ptr ds:[01B4303Ch]

push 0Ah

push 0Ah

[/code]

Remember, the first two arguments are passed in ECX and EDX, respectively. Our this pointer (constructed above with the Foo f = new Foo() C# code) resides in ESI, and thus we simply mov it into ECX. Then we move the pointer to the string “Hi” into EDX; the exact address clearly will change based on your program. Since we are passing two additional parameters to the method beyond the two which are stored in a register, we pass them using the machine’s stack; 0Ah is hexadecimal for the integer 10, so we push two onto the stack (one each for each argument).

Lastly, we make a call to a statically known address. This address refers to the appropriate method-table slot, in this case Foo::f’s, and is discovered at JIT compile time by matching the supplied method token with the internal CLR method-table data structure:

[code lang=csharp]

call FFFC0D28

[/code]

The second call — through the b variable — differs only in that it passes b’s value in the ECXregister. The target address of the call is the same:

[code lang=csharp]

mov ecx,edi

mov edx,dword ptr ds:[01B4303Ch]

push 0Ah

push 0Ah

call FFFC0D28

[/code]

After performing the call to FFFC0D28 in this example, the stub will either jmp straight to the jitted code or invoke the JIT compiler (with a call) as needed.

### Virtual Method Calls (callvirt)

A virtual method is very much like ordinary calls, except that it must look up the target of the call at runtime based on the this object. For example, consider this code:

[code lang=csharp]

int fg = f.g("Hi", 10, 10);

int bg = b.g("Hi", 10, 10);

[/code]

The manner in which the this pointer and its arguments are passed is identical to the call example above. ESI is moved into ECX for the dispatch on f and EDI is moved into ECX for the dispatch on b. The difference is that the call target can’t be burned into the call-site. Instead, we use the method-table to get at the address:

[code lang=csharp]

mov eax,dword ptr [ecx]

call dword ptr [eax+38h]

[/code]

We first dereference ECX, which holds the this pointer, and store the result in EAX. Then we add38h to EAX to get at the correct slot in the vtable. Because this vtable was discovered using the thispointer, the address will differ for f and b, and the call through b will end up going through the override. We call the address of that slot. Remember, we stated above that all classes in a hierarchy use the same offsets for methods, meaning that this same offset can be used for all derived classes.

The full IA-32 for this calling sequence (using the f pointer) is:

[code lang=csharp]

mov ecx,esi

mov edx,dword ptr ds:[01B4303Ch]

push 0Ah

push 0Ah

mov eax,dword ptr [ecx]

call dword ptr [eax+38h]

[/code]

Again, the only difference when b is used is that EDI, instead of ESI, is moved into ECX.

### Indirect Method Calls (calli)

C# doesn’t supply a mechanism with which to emit a calli instruction in the IL. You can, of course, emit code using the Reflection.Emit namespace (described in Chapter 14), but an example would introduce more complexity than necessary. If you were to imagine that a calli sequence were being JIT compiled, the only difference introduced would be that the native call instruction would perform a call dword ptr [exx], where exx is the register in which the target address of the calli was found; that is, it calls the address to which the indirect pointer refers. All of the arguments would be passed in accordance to the method token supplied to the calli instruction.

### Dynamic Method Calls (Delegates, Others)

There is a range of dynamic method calls available. Many of them are part of the dynamic programming infrastructure — discussed in depth in Chapter 14 — and thus won’t be explored in depth here. They are all variants on the same basic premise, which is that some piece of runtime functionality is able to look up the method-table information at runtime to make a method dispatch.

Delegates were described in detail in Chapter 2. We’ll use them as the basis for our discussion here. Recall that a delegate is essentially a strongly typed function pointer type, an instance of which has two pieces of information: the target object (to be passed as this), and the target method. Each delegate type has a special Invoke method whose signature matches the function over which it has been formed. The CLR supplies the implementation of this method, which enables it to perform lightweight dispatch to the underlying method.

A call to a delegate looks identical to a call to a normal method. The difference is that the target is the delegate’s Invoke method-table slot instead of the underlying function. Arguments are laid out as with any other type of call (i.e.,_fastcall). The implementation of Invoke simply patches the ECXregister to contain the target object reference (supplied at delegate construction time) and uses the method token (also supplied at delegate construction time) to jump to the appropriate method-slot. There is very little overhead in this process, which makes delegate dispatch on the order of zero to one times slower than a simple virtual method call.

### A Word on More Dynamic Invocation Mechanisms

The various other styles of method dispatch — such as Type.InvokeMember, MethodInfo.Invoke, and so forth — all add a certain level of overhead for binding to the target method. Delegates don’t ordinarily suffer from this because the method token is embedded in the IL. You may dynamically construct and invoke delegates (e.g., with DynamicInvoke), which does add a comparable level of overhead for the construction and binding. Lastly, the more dynamic mechanisms listed above tend to pass arguments in as object[]s, meaning that the dispatching code inside the CLR must transform that into the appropriate calling convention to perform the invocation (and then do the necessary marshaling on the return).

### Prologues and Epilogues

Every method is responsible for performing a set of actions (called a _prologue_ and _epilogue_) to set up the activation frame and return back to the caller at the end. The fastcall convention dictates that the callee is responsible for “cleaning the stack”; this simply means that the caller must ensure that any modifications to the stack (e.g., for arguments) have been restored prior to the return. In summary, this process involves:

  * The current stack address is saved (which is a back-link to the caller’s stack frame) by pushing the EBP (base pointer) address onto the stack. The current value of ESI (stack pointer, implicitly used by push, pop, call, ret) is then stored in EBP, forming the beginning of the called method’s new activation frame. This process enables the callee to later restore the stack during the prologue to its position just prior to the method call simply by popping the address back into EBP.
  * If the function intends to modify registers EDI or ESI (among others), it must save them on its stack during the prologue and restore them in the epilogue.
  * ESP is decremented by a number of bytes (the stack grows downward), equal to the number of bytes necessary to store the method’s local variables. Local variables are initialized, usually by just storing 0 into the various offsets relative to EBP, for example mov dword ptr [ebp-10h],0.
  * The body of the method is then executed. It references items passed and stored on the stack using an offset from EBP, for example arguments and locals, throughout its method body.
  * The callee “cleans the stack” by restoring the EBP and ESP stack pointers to their previous values, essentially doing the reverse of what the prologue did. It uses the ret instruction to restore the previous EIP (which call implicitly saved on the stack) and (optionally) returns a value to the caller.
  * Execution continues as normal at the next instruction (EIP) after the call was made.

Throughout the process of executing method calls, the physical stack is growing and shrinking (which simply means the EBP and ESP pointers refer to varying locations on the stack), and causes activation frames to be conceptually pushed and popped off (by the prologues and epilogues).

### Viewing Stack Frames

The whole process of constructing activation frames is visible at a high level when you view a stack trace, for example in the debugger, resulting from an exception, or by capturing one manually:

[code lang=csharp]

using System;

using System.Diagnostics;

class Foo

{

static void Main()

{

A();

}

static void A()

{

B(10, 50);

}

static void B(int x, int y)

{

C("Hello", x * y);

}

static void C(string label, int num)

{

StackTrace trace = new StackTrace();

Console.WriteLine(trace.ToString());

}

}

[/code]

Running this program prints out some information about the chain of activation frames leading up to the new StackTrace() statement in C:

[code lang=csharp]

at Foo.C(String label, Int32 num) in c:\…\stack.cs:line 23

at Foo.B(Int32 x, Int32 y) in c:\…\stack.cs:line 18

at Foo.A() in c:\…\stack.cs:line 13

at Foo.Main() in c:\…\stack.cs:line 8

[/code]

Most of the interesting information is not captured in this view — you’ll have to drop down into a debugger to get the low-level details — but this at least demonstrates the high-level bits of information.

### 64-Bit Support

Versions 1.0 and 1.1 of the CLR only had a JIT Compiler that produced code targeting 32-bit x86 and IA-32 instruction sets. With the introduction of 2.0, the JIT Compiler also produces code to target Intel’s 64-bit Itanium family of processors (IA-64), and AMD’s 64-bit family of processors (AMD-64 or x64). AMD’s instruction set is actually very similar to the IA-32 (a.k.a. x86) instruction set, of course, with widened storage for 64-bit native data. The Itanium IA-64 instruction set, on the other hand, is vastly different. A full description of these differences is entirely beyond the scope of this book.


text:
import { Task, taskViewService } from '../statuses';

export function forceTaskStop(task: Task) {
  if (task instanceof Task) {
    // See https://github.com/Microsoft/vscode-docs/blob/master/docs/extensionAPI/extension-points.md#contributesmenus
    // For best case inference efforts on what to pass in
    taskViewService.terminateTask(task);
  } else {
    taskViewService.terminateTask();
  }
}


text:
package edu.mit.streamjit.api;

import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * A WeightedRoundrobinJoiner joins its input by taking data items from its
 * children according to specified weights.  A WeightedRoundrobinJoiner with
 * weights [1, 2, 1] will take one item from its first parent, two items from
 * its second parent, and one item from its third parent per iteration.
 *
 * TODO: see WeightedRoundrobinSplitter for details about this class' necessity
 * @author Jeffrey Bosboom <jbosboom@csail.mit.edu>
 * @since 11/7/2012
 */
public final class WeightedRoundrobinJoiner<T> extends Joiner<T, T> {
	private final int[] weights;
	public WeightedRoundrobinJoiner(int... weights) {
		this.weights = weights;
	}

	@Override
	public void work() {
		for (int i = 0; i < inputs(); ++i)
			for (int j = 0; j < weights[i]; ++j)
				push(pop(i));
	}

	@Override
	public int supportedInputs() {
		return weights.length;
	}

	@Override
	public List<Rate> getPeekRates() {
		//We don't peek.
		return Collections.nCopies(inputs(), Rate.create(0));
	}

	@Override
	public ImmutableList<Rate> getPopRates() {
		ImmutableList.Builder<Rate> r = ImmutableList.builder();
		for (int w : weights)
			r.add(Rate.create(w));
		return r.build();
	}

	@Override
	public ImmutableList<Rate> getPushRates() {
		int sum = 0;
		for (int w : weights)
			sum += w;
		return ImmutableList.of(Rate.create(sum));
	}

	@Override
	public String toString() {
		return String.format("WeightedRoundrobinJoiner(%s)", Arrays.toString(weights));
	}
}


text:
#include <Foundation/common.h>
#include <Foundation/NSString.h>
#include <Foundation/NSObjCRuntime.h>
#include <Foundation/NSUtilities.h>

#include <extensions/objc-runtime.h>

/*
 * Objective-C runtime info
 */

LF_DECLARE Class NSClassFromString(NSString *aClassName)
{
    unsigned len = [aClassName cStringLength];
    char  *buf;
    Class clazz;
    if (len == 0) return NULL;
    buf = malloc(len + 1);
    [aClassName getCString:buf]; buf[len] = '\0';
    clazz = objc_lookup_class(buf);
    free(buf);
    return clazz;
}

LF_DECLARE SEL NSSelectorFromString(NSString *aSelectorName)
{
    unsigned len = [aSelectorName cStringLength];
    char *buf;
    SEL  sel;
    if (len == 0) return NULL;
    buf = malloc(len + 1);
    [aSelectorName getCString:buf]; buf[len] = '\0';
    sel = sel_get_any_uid(buf);
    free(buf);
    return sel;
}

LF_DECLARE NSString *NSStringFromClass(Class aClass)
{
    return aClass ?
	[NSString stringWithCStringNoCopy:(char*)class_get_class_name(aClass)
	    freeWhenDone:NO] : nil;
}

LF_DECLARE NSString *NSStringFromSelector(SEL aSelector)
{
    if (aSelector) {
        return [NSString stringWithCStringNoCopy:(char*)sel_get_name(aSelector)
                         freeWhenDone:NO];
    }
    else {
        NSLog(@"WARNING: called %s with NULL-selector !", __PRETTY_FUNCTION__);
        return nil;
    }
}

/*
  Local Variables:
  c-basic-offset: 4
  tab-width: 8
  End:
*/


text:
"""Tests for lit_nlp.components.ablation_flip."""

from typing import Iterable, Iterator

from absl.testing import absltest
from lit_nlp.api import types
from lit_nlp.components import ablation_flip
from lit_nlp.examples.models import glue_models
import numpy as np

# TODO(lit-dev): Move glue_models out of lit_nlp/examples

BERT_TINY_PATH = 'https://storage.googleapis.com/what-if-tool-resources/lit-models/sst2_tiny.tar.gz'  # pylint: disable=line-too-long
STSB_PATH = 'https://storage.googleapis.com/what-if-tool-resources/lit-models/stsb_tiny.tar.gz'  # pylint: disable=line-too-long
import transformers
BERT_TINY_PATH = transformers.file_utils.cached_path(BERT_TINY_PATH,
  extract_compressed_file=True)
STSB_PATH = transformers.file_utils.cached_path(STSB_PATH,
  extract_compressed_file=True)

class SST2ModelNonRequiredField(glue_models.SST2Model):

  def input_spec(self):
    spec = super().input_spec()
    spec['sentence'] = types.TextSegment(required=False, default='')
    return spec

class SST2ModelWithPredictCounter(glue_models.SST2Model):

  def __init__(self, *args, **kw):
    super().__init__(*args, **kw)
    self.predict_counter = 0

  def predict(self,
              inputs: Iterable[types.JsonDict],
              scrub_arrays=True,
              **kw) -> Iterator[types.JsonDict]:
    results = super().predict(inputs, scrub_arrays, **kw)
    self.predict_counter += 1
    return results

class ModelBasedAblationFlipTest(absltest.TestCase):

  def setUp(self):
    super(ModelBasedAblationFlipTest, self).setUp()
    self.ablation_flip = ablation_flip.AblationFlip()

    # Classification model that clasifies a given input sentence.
    self.classification_model = glue_models.SST2Model(BERT_TINY_PATH)
    self.classification_config = {ablation_flip.PREDICTION_KEY: 'probas'}

    # Clasification model with the 'sentence' field marked as
    # non-required.
    self.classification_model_non_required_field = SST2ModelNonRequiredField(
        BERT_TINY_PATH)

    # Clasification model with a counter to count number of predict calls.
    # TODO(ataly): Consider setting up a Mock object to count number of
    # predict calls.
    self.classification_model_with_predict_counter = (
        SST2ModelWithPredictCounter(BERT_TINY_PATH))

    # Regression model determining similarity between two input sentences.
    self.regression_model = glue_models.STSBModel(STSB_PATH)
    self.regression_config = {ablation_flip.PREDICTION_KEY: 'score'}

  def test_ablation_flip_num_ex(self):
    ex = {'sentence': 'this long movie was terrible'}
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 0
    self.assertEmpty(
        self.ablation_flip.generate(ex, self.classification_model, None,
                                    self.classification_config))
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 1
    self.assertLen(
        self.ablation_flip.generate(ex, self.classification_model, None,
                                    self.classification_config), 1)
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 2
    self.assertLen(
        self.ablation_flip.generate(ex, self.classification_model, None,
                                    self.classification_config), 2)

  def test_ablation_flip_num_ex_multi_input(self):
    ex = {'sentence1': 'this long movie is terrible',
          'sentence2': 'this short movie is great'}
    self.regression_config[ablation_flip.NUM_EXAMPLES_KEY] = 2
    thresh = 2
    self.regression_config[ablation_flip.REGRESSION_THRESH_KEY] = thresh
    self.assertLen(
        self.ablation_flip.generate(ex, self.regression_model, None,
                                    self.regression_config), 2)

  def test_ablation_flip_long_sentence(self):
    sentence = (
        'this was a terrible terrible movie but I am a writing '
        'a nice long review for testing whether AblationFlip '
        'can handle long sentences with a bounded number of '
        'predict calls.')
    ex = {'sentence': sentence}
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 100
    self.classification_config[ablation_flip.MAX_ABLATIONS_KEY] = 100
    model = self.classification_model_with_predict_counter
    cfs = self.ablation_flip.generate(
        ex, model, None, self.classification_config)

    # This example must yield 19 ablation_flips.
    self.assertLen(cfs, 19)

    # Number of predict calls made by ablation_flip should be upper-bounded by
    # <number of tokens in sentence> + 2**MAX_ABLATABLE_TOKENS
    num_tokens = len(model.tokenizer(sentence))
    num_predict_calls = model.predict_counter
    self.assertLessEqual(num_predict_calls,
                         num_tokens + 2**ablation_flip.MAX_ABLATABLE_TOKENS)

    # We use a smaller value of MAX_ABLATABLE_TOKENS and check that the
    # number of predict calls is smaller, and that the prediction bound still
    # holds.
    model.predict_counter = 0
    ablation_flip.MAX_ABLATABLE_TOKENS = 5
    self.assertLessEqual(model.predict_counter, num_predict_calls)
    self.assertLessEqual(model.predict_counter,
                         num_tokens + 2**ablation_flip.MAX_ABLATABLE_TOKENS)

  def test_ablation_flip_freeze_fields(self):
    ex = {'sentence1': 'this long movie is terrible',
          'sentence2': 'this long movie is great'}
    self.regression_config[ablation_flip.NUM_EXAMPLES_KEY] = 10
    thresh = 2
    self.regression_config[ablation_flip.REGRESSION_THRESH_KEY] = thresh
    self.regression_config[ablation_flip.FIELDS_TO_ABLATE_KEY] = [
        'sentence1'
    ]
    cfs = self.ablation_flip.generate(ex, self.regression_model, None,
                                      self.regression_config)
    for cf in cfs:
      self.assertEqual(cf['sentence2'], ex['sentence2'])

  def test_ablation_flip_max_ablations(self):
    ex = {'sentence': 'this movie is terrible'}
    ex_tokens = self.ablation_flip.tokenize(ex['sentence'])
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 1
    self.classification_config[ablation_flip.MAX_ABLATIONS_KEY] = 1
    cfs = self.ablation_flip.generate(
        ex, self.classification_model, None, self.classification_config)
    cf_tokens = self.ablation_flip.tokenize(list(cfs)[0]['sentence'])
    self.assertLen(cf_tokens, len(ex_tokens) - 1)

    ex = {'sentence': 'this long movie is terrible and horrible.'}
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 1
    self.classification_config[ablation_flip.MAX_ABLATIONS_KEY] = 1
    cfs = self.ablation_flip.generate(
        ex, self.classification_model, None, self.classification_config)
    self.assertEmpty(cfs)

  def test_ablation_flip_max_ablations_multi_input(self):
    ex = {'sentence1': 'this movie is terrible',
          'sentence2': 'this movie is great'}
    ex_tokens1 = self.ablation_flip.tokenize(ex['sentence1'])
    ex_tokens2 = self.ablation_flip.tokenize(ex['sentence2'])

    self.regression_config[ablation_flip.NUM_EXAMPLES_KEY] = 20
    self.regression_config[ablation_flip.REGRESSION_THRESH_KEY] = 2
    max_ablations = 1
    self.regression_config[ablation_flip.MAX_ABLATIONS_KEY] = max_ablations
    cfs = self.ablation_flip.generate(ex, self.regression_model, None,
                                      self.regression_config)
    for cf in cfs:
      # Number of ablations in each field should be no more than MAX_ABLATIONS.
      cf_tokens1 = self.ablation_flip.tokenize(cf['sentence1'])
      cf_tokens2 = self.ablation_flip.tokenize(cf['sentence2'])
      self.assertGreaterEqual(
          len(cf_tokens1) + len(cf_tokens2),
          len(ex_tokens1) + len(ex_tokens2) - max_ablations)

  def test_ablation_flip_yields_multi_field_ablations(self):
    ex = {'sentence1': 'this short movie is awesome',
          'sentence2': 'this short movie is great'}
    ex_tokens1 = self.ablation_flip.tokenize(ex['sentence1'])
    ex_tokens2 = self.ablation_flip.tokenize(ex['sentence2'])

    self.regression_config[ablation_flip.NUM_EXAMPLES_KEY] = 20
    self.regression_config[ablation_flip.REGRESSION_THRESH_KEY] = 2
    self.regression_config[ablation_flip.MAX_ABLATIONS_KEY] = 5
    cfs = self.ablation_flip.generate(ex, self.regression_model, None,
                                      self.regression_config)

    # Verify that at least one counterfactual involves ablations across
    # multiple fields.
    multi_field_ablation_found = False
    for cf in cfs:
      cf_tokens1 = self.ablation_flip.tokenize(cf['sentence1'])
      cf_tokens2 = self.ablation_flip.tokenize(cf['sentence2'])
      if ((len(cf_tokens1) < len(ex_tokens1))
          and (len(cf_tokens2) < len(ex_tokens2))):
        multi_field_ablation_found = True
        break
    self.assertTrue(multi_field_ablation_found)

  def test_ablation_flip_changes_pred_class(self):
    ex = {'sentence': 'this long movie is terrible'}
    ex_output = list(self.classification_model.predict([ex]))[0]
    pred_class = str(np.argmax(ex_output['probas']))
    self.assertEqual('0', pred_class)
    cfs = self.ablation_flip.generate(ex, self.classification_model, None,
                                      self.classification_config)
    cf_outputs = self.classification_model.predict(cfs)
    for cf_output in cf_outputs:
      self.assertNotEqual(np.argmax(ex_output['probas']),
                          np.argmax(cf_output['probas']))

  def test_ablation_flip_changes_regression_score(self):
    ex = {'sentence1': 'this long movie is terrible',
          'sentence2': 'this short movie is great'}
    self.regression_config[ablation_flip.NUM_EXAMPLES_KEY] = 2
    ex_output = list(self.regression_model.predict([ex]))[0]
    thresh = 2
    self.regression_config[ablation_flip.REGRESSION_THRESH_KEY] = thresh
    cfs = self.ablation_flip.generate(ex, self.regression_model, None,
                                      self.regression_config)
    cf_outputs = self.regression_model.predict(cfs)
    for cf_output in cf_outputs:
      self.assertNotEqual((ex_output['score'] <= thresh),
                          (cf_output['score'] <= thresh))

  def test_ablation_flip_fails_without_pred_key(self):
    ex = {'sentence': 'this long movie is terrible'}
    with self.assertRaises(AssertionError):
      self.ablation_flip.generate(ex, self.classification_model, None, None)

  def test_ablation_flip_required_field(self):
    ex = {'sentence': 'terrible'}
    self.classification_config[ablation_flip.NUM_EXAMPLES_KEY] = 1
    self.assertEmpty(
        self.ablation_flip.generate(
            ex, self.classification_model, None, self.classification_config))
    self.assertLen(
        self.ablation_flip.generate(
            ex, self.classification_model_non_required_field,
            None, self.classification_config), 1)

if __name__ == '__main__':
  absltest.main()


text:
package play.cache;

import play.libs.F;
import play.mvc.*;
import play.mvc.Http.*;

/**
 * Cache another action.
 */
public class CachedAction extends Action<Cached> {

    public F.Promise<SimpleResult> call(Context ctx) {
        try {
            final String key = configuration.key();
            final Integer duration = configuration.duration();
            SimpleResult result = (SimpleResult) Cache.get(key);
            F.Promise<SimpleResult> promise;
            if(result == null) {
                promise = delegate.call(ctx);
                promise.onRedeem(new F.Callback<SimpleResult>() {
                    @Override
                    public void invoke(SimpleResult simpleResult) throws Throwable {
                        Cache.set(key, simpleResult, duration);
                    }
                });
            } else {
                promise = F.Promise.pure(result);
            }
            return promise;
        } catch(RuntimeException e) {
            throw e;
        } catch(Throwable t) {
            throw new RuntimeException(t);
        }
    }

}


text:
<?xml version="1.0" encoding="UTF-8"?>
<Tokens version="1.0">
	<File path="Classes/SimpleDBListDomainsResponse.html">

		<Token>
			<TokenIdentifier>//apple_ref/occ/cl/SimpleDBListDomainsResponse</TokenIdentifier>
			<Abstract type="html">List Domains Result</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/setException:</TokenIdentifier>
			<Abstract type="html">If an exception was thrown, this will return the exception.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>- (void)setException:(AmazonServiceException *)theException</Declaration>

			<Anchor>//api/name/setException:</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/init</TokenIdentifier>
			<Abstract type="html">Default constructor for a new  object.  Callers should use the
property methods to initialize this object after creating it.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>- (id)init</Declaration>

			<Anchor>//api/name/init</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/setDomainNames:</TokenIdentifier>
			<Abstract type="html">A list of domain names that match the expression.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>@property (nonatomic, retain) NSMutableArray *domainNames</Declaration>

			<Anchor>//api/name/domainNames</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/domainNames</TokenIdentifier>
			<Abstract type="html">A list of domain names that match the expression.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>@property (nonatomic, retain) NSMutableArray *domainNames</Declaration>

			<Anchor>//api/name/domainNames</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instp/SimpleDBListDomainsResponse/domainNames</TokenIdentifier>
			<Abstract type="html">A list of domain names that match the expression.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>@property (nonatomic, retain) NSMutableArray *domainNames</Declaration>

			<Anchor>//api/name/domainNames</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/setNextToken:</TokenIdentifier>
			<Abstract type="html">An opaque token indicating that there are more domains than the
specified MaxNumberOfDomains still available.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>@property (nonatomic, retain) NSString *nextToken</Declaration>

			<Anchor>//api/name/nextToken</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/nextToken</TokenIdentifier>
			<Abstract type="html">An opaque token indicating that there are more domains than the
specified MaxNumberOfDomains still available.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>@property (nonatomic, retain) NSString *nextToken</Declaration>

			<Anchor>//api/name/nextToken</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instp/SimpleDBListDomainsResponse/nextToken</TokenIdentifier>
			<Abstract type="html">An opaque token indicating that there are more domains than the
specified MaxNumberOfDomains still available.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>@property (nonatomic, retain) NSString *nextToken</Declaration>

			<Anchor>//api/name/nextToken</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/domainNamesObjectAtIndex:</TokenIdentifier>
			<Abstract type="html">Returns a value from the domainNames array for the specified index</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>- (NSString *)domainNamesObjectAtIndex:(int)index</Declaration>

			<Anchor>//api/name/domainNamesObjectAtIndex:</Anchor>
            <NodeRef refid="936"/>
		</Token>

		<Token>
			<TokenIdentifier>//apple_ref/occ/instm/SimpleDBListDomainsResponse/description</TokenIdentifier>
			<Abstract type="html">Returns a string representation of this object; useful for testing and
debugging.</Abstract>
			<DeclaredIn>SimpleDBListDomainsResponse.h</DeclaredIn>

			<Declaration>- (NSString *)description</Declaration>

			<ReturnValue><Abstract type="html">A string representation of this object.</Abstract></ReturnValue>
			<Anchor>//api/name/description</Anchor>
            <NodeRef refid="936"/>
		</Token>

	</File>
</Tokens>


text:
package farm.bsg.pages;

import org.junit.Assert;
import org.junit.Test;

import farm.bsg.PageBootstrap;
import farm.bsg.PageTesting;

public class PeopleTest extends PageTesting {

    @Test
    public void Coverage() throws Exception {
        final PageBootstrap bootstrap = go();
        final String html = bootstrap.GET("/people", params());
        Assert.assertNotNull(html);
    }
}


text:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

using MongoDB.Bson.Serialization.Attributes;

namespace MobiStore.Models.MobileDevices.Components
{
    public class Processor
    {
        private ICollection<MobileDevice> mobileDevices;

        public Processor()
        {
            this.Id = Guid.NewGuid();
            this.mobileDevices = new HashSet<MobileDevice>();
        }

        public Guid Id { get; set; }

        [Required]
        public double ClockSpeed { get; set; }

        [Required]
        public double CacheMemory { get; set; }

        [BsonIgnore]
        public virtual ICollection<MobileDevice> MobileDevices
        {
            get { return this.mobileDevices; }

            set { this.mobileDevices = value; }
        }
    }
}


text:
<!DOCTYPE html>

  <title>CSS 2.1 Test Suite: :first-letter</title>
  <link rel="author" title="Boris Zbarsky" href="mailto:bzbarsky@mit.edu" />
  <link rel="author" title="Mozilla Corporation" href="http://mozilla.com/" />
  <link rel="help" href="http://www.w3.org/TR/CSS21/selector.html#first-letter"/>
  <meta name="flags" content="dom" />


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
SurgeryMigrator
</TITLE>

<META NAME="keywords" CONTENT="gov.nih.nci.cabig.caaers.datamigrator.SurgeryMigrator class">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

gov.nih.nci.cabig.caaers.datamigrator

Class SurgeryMigrator

java.lang.Object
  gov.nih.nci.cabig.caaers.datamigrator.CaaersDataMigratorTemplate
      gov.nih.nci.cabig.caaers.datamigrator.SurgeryMigrator

All Implemented Interfaces:
CaaersDataMigrator

public class SurgeryMigrator
extends CaaersDataMigratorTemplate

Does the migration of Surgery

Nested Class Summary

Nested classes/interfaces inherited from class gov.nih.nci.cabig.caaers.datamigrator.CaaersDataMigratorTemplate

CaaersDataMigratorTemplate.CaaersDataMigrationContext

Nested classes/interfaces inherited from interface gov.nih.nci.cabig.caaers.datamigrator.CaaersDataMigrator

CaaersDataMigrator.MigraorType

Field Summary

Fields inherited from class gov.nih.nci.cabig.caaers.datamigrator.CaaersDataMigratorTemplate

log

Constructor Summary

SurgeryMigrator()

Method Summary

 java.util.ArrayList<java.lang.String>
generateOtherStudyInterventionInsertSQL(CaaersDataMigratorTemplate.CaaersDataMigrationContext ctx,
                                        java.util.HashSet<java.lang.String> set)

 java.util.ArrayList<java.lang.String>
generateSurgeryInterventionUpdateSQL(java.util.HashMap<java.lang.String,java.lang.String> map)

 void
migrate(CaaersDataMigratorTemplate.CaaersDataMigrationContext ctx)

 CaaersDataMigrator.MigraorType
migratorType()

          A unique ID for the migator, which will be used while loging.

Methods inherited from class gov.nih.nci.cabig.caaers.datamigrator.CaaersDataMigratorTemplate

afterMigrate, beforeMigrate, getJdbcTemplate, migrateData, setJdbcTemplate

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

SurgeryMigrator

public SurgeryMigrator()

Method Detail

migratorType

public CaaersDataMigrator.MigraorType migratorType()

Description copied from interface: CaaersDataMigrator

A unique ID for the migator, which will be used while loging.

Returns:

migrate

public void migrate(CaaersDataMigratorTemplate.CaaersDataMigrationContext ctx)

Overrides:
migrate in class CaaersDataMigratorTemplate

generateOtherStudyInterventionInsertSQL

public java.util.ArrayList<java.lang.String> generateOtherStudyInterventionInsertSQL(CaaersDataMigratorTemplate.CaaersDataMigrationContext ctx,
                                                                                java.util.HashSet<java.lang.String> set)

generateSurgeryInterventionUpdateSQL

public java.util.ArrayList<java.lang.String> generateSurgeryInterventionUpdateSQL(java.util.HashMap<java.lang.String,java.lang.String> map)

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD


text:
@import Foundation;

@interface OneWordSuperclass : NSObject
@end


text:
/**
 * Contains all spring configuration classes.
 */
package com.faforever.api.config;


text:
<?php
namespace FACTFinder\Core\Server;

use FACTFinder\Loader as FF;

/**
 * Represents all data belonging to a single connection. It holds all data
 * necessary to configure a request and can also be filled with the
 * corresponding response.
 */
class ConnectionData
{
    /**
     * @var \FACTFinder\Util\Parameters
     */
    private $httpHeaderFields;

    /**
     * @var string
     */
    private $action;

    /**
     * @var \FACTFinder\Util\Parameters
     */
    private $parameters;

    /**
     * @var mixed[]
     */
    private $connectionOptions = array();

    /**
     * @var Response
     */
    private $response;

    /**
     * @var string
     */
    private $previousUrl;

    /**
     * Optionally takes a Parameters object to initialize the query parameters.
     * @param \FACTFinder\Util\Parameters $parameters
     * @return type
     */
    public function __construct($parameters = null)
    {
        if (FF::isInstanceOf($parameters, 'Util\Parameters'))
            $this->parameters = $parameters;
        else
            $this->parameters = FF::getInstance('Util\Parameters');

        $this->httpHeaderFields = FF::getInstance('Util\Parameters');
        $this->action = '';
        $this->setNullResponse();
    }

    /**
     * Returns the parameters object used for the connection, on which HTTP
     * header fields can be read and changed.
     *
     * @return \FACTFinder\Util\Parameters
     */
    public function getHttpHeaderFields()
    {
        return $this->httpHeaderFields;
    }

    /**
     * Set the action to be queried on the FACT-Finder server. e.g. "Search.ff".
     *
     * @param string $action
     */
    public function setAction($action)
    {
        $this->action = $action;
    }

    /**
     * Get the action to be queried on the FACT-Finder server. e.g. "Search.ff".
     *
     * @return string
     */
    public function getAction()
    {
        return $this->action;
    }

    /**
     * Returns the parameters object used for the connection, on which query
     * parameters can be read and changed.
     *
     * @return \FACTFinder\Util\Parameters
     */
    public function getParameters()
    {
        return $this->parameters;
    }

    /**
     * Set an option for the connection. This can be an arbitrary pair of key
     * and value. How this is used depends on the kind of connection that is set
     * up (e.g. cURL or socket). We simply provide an array to store options
     * specific to this connection.
     *
     * @param mixed $name The option's identifier. This can be any value that is
     *        a valid array index in PHP (hence, it SHOULD be a string or an
     *        integer; all other types will be interpreted as a string).
     * @param mixed $value The option's value.
     */
    public function setConnectionOption($name, $value)
    {
        $this->connectionOptions[$name] = $value;
    }

    /**
     * Sets all given connection options at once. If an option with the same
     * name already exists, the old value will be replaced. Options that are not
     * contained in the $options parameter will retain their value(s).
     * @param mixed[] $options An array of options. For more information on
     *        valid keys and values see setConnectionOption($name, $value).
     */
    public function setConnectionOptions($options)
    {
        // We cannot use array_merge() here, because that does not preserve
        // numeric keys. Implementing this with a loop also has the advantage
        // of not creating a new, third array.
        foreach ($options as $k => $v)
            $this->connectionOptions[$k] = $v;
    }

    /**
     * @param mixed $name The option's identifier.
     * @return bool
     */
    public function issetConnectionOption($name)
    {
        return isset($this->connectionOptions[$name]);
    }

    /**
     * @param mixed $name The option's identifier.
     * @return mixed The option's value.
     */
    public function getConnectionOption($name)
    {
        return $this->connectionOptions[$name];
    }

    /**
     * Get an array of all set connection options.
     * @return mixed[]
     */
    public function getConnectionOptions()
    {
        return $this->connectionOptions;
    }

    /**
     * Set a response for the current connection settings along with the URL
     * which was used to obtain the response.
     * @param Response $response
     * @param string $url The URL corresponding to $response.
     */
    public function setResponse(Response $response, $url)
    {
        $this->response = $response;
        $this->previousUrl = $url;
    }

    /**
     * Set a null response - e.g. if the connection could not even be attempted
     * at all.
     */
    public function setNullResponse()
    {
        $this->response = FF::getInstance('Core\Server\NullResponse');
        $this->previousUrl = null;
    }

    /**
     * Get the response that was most recently set.
     *
     * @return Response
     */
    public function getResponse()
    {
        return $this->response;
    }

    /**
     * Get the URL corresponding to the currently stored Response.
     *
     * @return string
     */
    public function getPreviousUrl()
    {
        return $this->previousUrl;
    }
}


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>de.codecentric</groupId>
	<artifactId>spring-hierarchical</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>spring-hierarchical</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.4</junit.version>
		<spring.version>3.2.4.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<version>1.9.5</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<artifactId>spring-context</artifactId>
			<groupId>org.springframework</groupId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>
</project>


text:
#include <stdio.h>
#include <string.h>

#include "eagle.h"
#include "channel_t.h"
#include "handlers.h"
#include "protocol.h"
#include "keylist.h"
#include "xmalloc.h"
#include "utils.h"

static void eject_clients_channel_t(Channel_t *channel);

static void free_channel_keylist_handler(void *key, void *value);
static int match_channel_keylist_handler(void *key1, void *key2);

Channel_t *create_channel_t(const char *name, uint32_t flags)
{
	Channel_t *channel = (Channel_t*)xmalloc(sizeof(*channel));

	memcpy(channel->name, name, strlenz(name));
	channel->flags = flags;

	channel->auto_delete = 0;
	channel->round_robin = 0;

	if (BIT_CHECK(channel->flags, EG_CHANNEL_AUTODELETE_FLAG)) {
		channel->auto_delete = 1;
	}

	if (BIT_CHECK(channel->flags, EG_CHANNEL_ROUND_ROBIN_FLAG)) {
		channel->round_robin = 1;
	}

	channel->topics = keylist_create();
	channel->patterns = keylist_create();

	EG_KEYLIST_SET_FREE_METHOD(channel->topics, free_channel_keylist_handler);
	EG_KEYLIST_SET_MATCH_METHOD(channel->topics, match_channel_keylist_handler);

	EG_KEYLIST_SET_FREE_METHOD(channel->patterns, free_channel_keylist_handler);
	EG_KEYLIST_SET_MATCH_METHOD(channel->patterns, match_channel_keylist_handler);

	return channel;
}

void delete_channel_t(Channel_t *channel)
{
	eject_clients_channel_t(channel);

	keylist_release(channel->topics);
	keylist_release(channel->patterns);

	xfree(channel);
}

void publish_message_channel_t(Channel_t *channel, const char *topic, Object *msg)
{
	EagleClient *client;
	KeylistIterator keylist_iterator;
	KeylistNode *keylist_node;
	ListNode *list_node;
	ListIterator list_iterator;
	List *list;
	char *pattern;

	keylist_node = keylist_get_value(channel->topics, (void*)topic);
	if (keylist_node)
	{
		list = EG_KEYLIST_NODE_VALUE(keylist_node);

		if (channel->round_robin)
		{
			list_rotate(list);

			client = EG_LIST_NODE_VALUE(EG_LIST_FIRST(list));
			channel_client_event_message(client, channel, topic, msg);
		}
		else
		{
			list_rewind(list, &list_iterator);
			while ((list_node = list_next_node(&list_iterator)) != NULL)
			{
				client = EG_LIST_NODE_VALUE(list_node);
				channel_client_event_message(client, channel, topic, msg);
			}
		}
	}

	if (EG_KEYLIST_LENGTH(channel->patterns))
	{
		keylist_rewind(channel->patterns, &keylist_iterator);
		while ((keylist_node = keylist_next_node(&keylist_iterator)) != NULL)
		{
			pattern = EG_KEYLIST_NODE_KEY(keylist_node);

			if (pattern_match(topic, pattern, 0))
			{
				list = EG_KEYLIST_NODE_VALUE(keylist_node);

				list_rewind(list, &list_iterator);
				while ((list_node = list_next_node(&list_iterator)) != NULL)
				{
					client = EG_LIST_NODE_VALUE(list_node);
					channel_client_event_pattern_message(client, channel, topic, pattern, msg);
				}
			}
		}
	}
}

Channel_t *find_channel_t(List *list, const char *name)
{
	Channel_t *channel;
	ListNode *node;
	ListIterator iterator;

	list_rewind(list, &iterator);
	while ((node = list_next_node(&iterator)) != NULL)
	{
		channel = EG_LIST_NODE_VALUE(node);
		if (!strcmp(channel->name, name)) {
			return channel;
		}
	}

	return NULL;
}

void rename_channel_t(Channel_t *channel, const char *name)
{
	memcpy(channel->name, name, strlenz(name));
}

static void add_keylist_channel_t(Keylist *keylist, Channel_t *channel, KeylistNode *keylist_node)
{
	List *list;
	KeylistNode *node = keylist_get_value(keylist, channel);
	int add = 0;

	if (!node)
	{
		list = list_create();
		keylist_set_value(keylist, channel, list);
		add = 1;
	}
	else
	{
		list = EG_KEYLIST_NODE_VALUE(node);
		if (!list_search_node(list, keylist_node))
			add = 1;
	}

	if (add)
	{
		list_add_value_tail(list, keylist_node);
	}
}

static int remove_keylist_channel_t(Keylist *keylist, Channel_t *channel, KeylistNode *keylist_node)
{
	List *list;
	KeylistNode *node = keylist_get_value(keylist, channel);

	if (!node) {
		return EG_STATUS_ERR;
	}

	list = EG_KEYLIST_NODE_VALUE(node);
	list_delete_value(list, keylist_node);

	if (!EG_LIST_LENGTH(list)) {
		list_release(list);
		keylist_delete_node(keylist, node);
	}

	return EG_STATUS_OK;
}

void subscribe_channel_t(Channel_t *channel, EagleClient *client, const char *topic)
{
	List *list;
	KeylistNode *node = keylist_get_value(channel->topics, (void*)topic);
	int subscribe = 0;

	if (!node)
	{
		list = list_create();
		keylist_set_value(channel->topics, xstrdup(topic), list);
		node = EG_KEYLIST_LAST(channel->topics);
		subscribe = 1;
	}
	else
	{
		list = EG_KEYLIST_NODE_VALUE(node);
		if (!list_search_node(list, client))
			subscribe = 1;
	}

	if (subscribe)
	{
		list_add_value_tail(list, client);
		add_keylist_channel_t(client->subscribed_topics, channel, node);
	}
}

void psubscribe_channel_t(Channel_t *channel, EagleClient *client, const char *pattern)
{
	List *list;
	KeylistNode *node = keylist_get_value(channel->patterns, (void*)pattern);
	int subscribe = 0;

	if (!node)
	{
		list = list_create();
		keylist_set_value(channel->patterns, xstrdup(pattern), list);
		node = EG_KEYLIST_LAST(channel->patterns);
		subscribe = 1;
	}
	else
	{
		list = EG_KEYLIST_NODE_VALUE(node);
		if (!list_search_node(list, client))
			subscribe = 1;
	}

	if (subscribe)
	{
		list_add_value_tail(list, client);
		add_keylist_channel_t(client->subscribed_patterns, channel, node);
	}
}

int unsubscribe_channel_t(Channel_t *channel, EagleClient *client, const char *topic)
{
	List *list;
	KeylistNode *node = keylist_get_value(channel->topics, (void*)topic);

	if (!node) {
		return EG_STATUS_ERR;
	}

	list = EG_KEYLIST_NODE_VALUE(node);

	list_delete_value(list, client);
	remove_keylist_channel_t(client->subscribed_topics, channel, node);

	if (!EG_LIST_LENGTH(list)) {
		keylist_delete_node(channel->topics, node);
	}

	if (channel->auto_delete)
	{
		if (EG_KEYLIST_LENGTH(channel->topics) == 0 &&
			EG_KEYLIST_LENGTH(channel->patterns) == 0) {
			list_delete_value(server->channels, channel);
		}
	}

	return EG_STATUS_OK;
}

int punsubscribe_channel_t(Channel_t *channel, EagleClient *client, const char *pattern)
{
	List *list;
	KeylistNode *node = keylist_get_value(channel->patterns, (void*)pattern);

	if (!node) {
		return EG_STATUS_ERR;
	}

	list = EG_KEYLIST_NODE_VALUE(node);

	list_delete_value(list, client);
	remove_keylist_channel_t(client->subscribed_patterns, channel, node);

	if (!EG_LIST_LENGTH(list)) {
		keylist_delete_node(channel->patterns, node);
	}

	if (channel->auto_delete)
	{
		if (EG_KEYLIST_LENGTH(channel->topics) == 0 &&
			EG_KEYLIST_LENGTH(channel->patterns) == 0) {
			list_delete_value(server->channels, channel);
		}
	}

	return EG_STATUS_OK;
}

static void eject_clients_channel_t(Channel_t *channel)
{
	ListIterator list_iterator;
	ListNode *list_node;
	KeylistIterator keylist_iterator;
	KeylistNode *keylist_node;
	EagleClient *client;
	List *list;

	keylist_rewind(channel->topics, &keylist_iterator);
	while ((keylist_node = keylist_next_node(&keylist_iterator)) != NULL)
	{
		list = EG_KEYLIST_NODE_VALUE(keylist_node);

		list_rewind(list, &list_iterator);
		while ((list_node = list_next_node(&list_iterator)) != NULL)
		{
			client = EG_LIST_NODE_VALUE(list_node);
			unsubscribe_channel_t(channel, client, EG_KEYLIST_NODE_KEY(keylist_node));
		}
	}

	keylist_rewind(channel->patterns, &keylist_iterator);
	while ((keylist_node = keylist_next_node(&keylist_iterator)) != NULL)
	{
		list = EG_KEYLIST_NODE_VALUE(keylist_node);

		list_rewind(list, &list_iterator);
		while ((list_node = list_next_node(&list_iterator)) != NULL)
		{
			client = EG_LIST_NODE_VALUE(list_node);
			punsubscribe_channel_t(channel, client, EG_KEYLIST_NODE_KEY(keylist_node));
		}
	}
}

void free_channel_list_handler(void *ptr)
{
	delete_channel_t(ptr);
}

static void free_channel_keylist_handler(void *key, void *value)
{
	xfree(key);
	list_release(value);
}

static int match_channel_keylist_handler(void *key1, void *key2)
{
	return !strcmp(key1, key2);
}


text:
'''
tests.test_util
'''

import threading
import multiprocessing

import atomos.util

def test_synchronized(thread_count=10, loop_count=1000):
    class SharedInt(object):
        value = 0

    shared_int = SharedInt()

    def inc(n):
        return n + 1

    @atomos.util.synchronized
    def inc_shared_int():
        for _ in range(loop_count):
            shared_int.value = inc(shared_int.value)

    threads = []
    for _ in range(thread_count):
        t = threading.Thread(target=inc_shared_int)
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    assert shared_int.value == thread_count * loop_count

def test_readers_writer_lock(acquire_shared_count=10):
    lock = atomos.util.ReadersWriterLock()

    for _ in range(acquire_shared_count):
        lock.shared.acquire()

    assert lock._reader_count == acquire_shared_count

    # Cannot acquire an exclusive lock with active readers.
    t = threading.Thread(target=lock.exclusive.acquire)
    t.start()

    t.join(1.0)

    assert t.is_alive() is True

    for _ in range(acquire_shared_count):
        lock.shared.release()

    assert lock._reader_count == 0

    t.join()

    assert t.is_alive() is False

    # Cannot acquire an exclusive lock with an active writer. (This was
    # acquired in the above thread.
    t = threading.Thread(target=lock.exclusive.acquire)
    t.start()

    t.join(1.0)

    assert t.is_alive() is True

    # Release the original acquisition.
    lock.exclusive.release()

    t.join()

    assert t.is_alive() is False

    # Cannot acquire a shared lock with an active writer. (This was acquired
    # in the above thread.)
    t = threading.Thread(target=lock.shared.acquire)
    t.start()

    t.join(1.0)

    assert t.is_alive() is True

    lock.exclusive.release()

    t.join()

    assert t.is_alive() is False
    assert lock._reader_count == 1

def test_readers_writer_lock_multiprocessing(acquire_shared_count=10):
    lock = atomos.util.ReadersWriterLockMultiprocessing()

    for _ in range(acquire_shared_count):
        lock.shared.acquire()

    assert lock._reader_count.value == acquire_shared_count

    # Cannot acquire an exclusive lock with active readers.
    p = multiprocessing.Process(target=lock.exclusive.acquire)
    p.start()

    p.join(1.0)

    assert p.is_alive() is True

    for _ in range(acquire_shared_count):
        lock.shared.release()

    assert lock._reader_count.value == 0

    p.join()

    assert p.is_alive() is False

    # Cannot acquire an exclusive lock with an active writer. (This was
    # acquired in the above process.
    p = multiprocessing.Process(target=lock.exclusive.acquire)
    p.start()

    p.join(1.0)

    assert p.is_alive() is True

    # Release the original acquisition.
    lock.exclusive.release()

    p.join()

    assert p.is_alive() is False

    # Cannot acquire a shared lock with an active writer. (This was acquired
    # in the above process.)
    p = multiprocessing.Process(target=lock.shared.acquire)
    p.start()

    p.join(1.0)

    assert p.is_alive() is True

    lock.exclusive.release()

    p.join()

    assert p.is_alive() is False
    assert lock._reader_count.value == 1


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
	                         http://maven.apache.org/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.doctor</groupId>
	<artifactId>openSourcLibrary-2015</artifactId>
	<packaging>jar</packaging>
	<version>1.0.0-SNAPSHOT</version>

	<name>openSourcLibrary-2015</name>
	<url>https://github.com/sdcuike,https://github.com/doctorwho1986/doctor</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.showWarnings>true</maven.compiler.showWarnings>
		<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>

		<wagon.version>2.9</wagon.version>
		<jsch.version>0.1.53</jsch.version>
		<javaslang.version>1.2.2</javaslang.version>
		<jool.version>0.9.6</jool.version>
		<joor.version>0.9.5</joor.version>
		<joou.version>0.9.1</joou.version>
		<httpclient.version>4.4.1</httpclient.version>
		<disruptor.version>3.3.2</disruptor.version>
		<mahout.version>0.9</mahout.version>
		<jsoup.version>1.8.2</jsoup.version>
		<avro.version>1.7.7</avro.version>

		<logback-classic.version>1.1.3</logback-classic.version>
		<log4j.version>1.2.17</log4j.version>
		<slf4j-api.version>1.7.12</slf4j-api.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>${wagon.version}</version>
		</dependency>

		<dependency>
			<groupId>com.jcraft</groupId>
			<artifactId>jsch</artifactId>
			<version>${jsch.version}</version>
		</dependency>

		<dependency>
			<groupId>ch.ethz.ganymed</groupId>
			<artifactId>ganymed-ssh2</artifactId>
			<version>262</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.6</version>
		</dependency>

		<dependency>
			<groupId>com.javaslang</groupId>
			<artifactId>javaslang</artifactId>
			<version>${javaslang.version}</version>
		</dependency>

		<dependency>
			<groupId>com.googlecode.grep4j</groupId>
			<artifactId>grep4j</artifactId>
			<version>1.8.7</version>
		</dependency>
		<!-- google.zxing -->
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>core</artifactId>
			<version>3.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.2.0</version>
		</dependency>
		<!-- google.zxing -->

		<dependency>
			<groupId>com.snowplowanalytics</groupId>
			<artifactId>referer-parser_2.11</artifactId>
			<version>0.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>jool</artifactId>
			<version>${jool.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>joor</artifactId>
			<version>${joor.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>joou</artifactId>
			<version>${joou.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>${httpclient.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>fluent-hc</artifactId>
			<version>${httpclient.version}</version>
		</dependency>

		<dependency>
			<groupId>com.lmax</groupId>
			<artifactId>disruptor</artifactId>
			<version>${disruptor.version}</version>
		</dependency>

		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>${jsoup.version}</version>
		</dependency>

		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.3.1</version>
		</dependency>

		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>18.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-collections4</artifactId>
			<version>4.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-proxy</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.mahout</groupId>
			<artifactId>mahout-core</artifactId>
			<version>${mahout.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.mahout</groupId>
			<artifactId>mahout-math</artifactId>
			<version>${mahout.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.mahout</groupId>
			<artifactId>mahout-integration</artifactId>
			<version>${mahout.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.mahout</groupId>
			<artifactId>mahout-examples</artifactId>
			<version>${mahout.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.avro</groupId>
			<artifactId>avro</artifactId>
			<version>${avro.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.avro</groupId>
			<artifactId>avro-ipc</artifactId>
			<version>${avro.version}</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j-api.version}</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback-classic.version}</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>${logback-classic.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j-api.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>openSourcLibrary-2015</finalName>
		<plugins>
		</plugins>
	</build>

	<reporting>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<compilerArgument>-parameters</compilerArgument>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>findbugs-maven-plugin</artifactId>
				<version>3.0.1</version>
				<configuration>
					<xmlOutput>true</xmlOutput>
					<!-- Optional directory to put findbugs xdoc xml report -->
					<xmlOutputDirectory>target/site</xmlOutputDirectory>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-pmd-plugin</artifactId>
				<version>3.4</version>
			</plugin>
		</plugins>
	</reporting>

	<repositories>
		<repository>
			<id>io.spring.repo.maven.release</id>
			<url>http://repo.spring.io/release/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>io.spring.repo.maven.milestone</id>
			<url>http://repo.spring.io/milestone/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>io.spring.repo.maven.snapshot</id>
			<url>http://repo.spring.io/snapshot/</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>

		<repository>
			<id>CN</id>
			<name>OSChina Central</name>
			<url>http://maven.oschina.net/content/groups/public/</url>
		</repository>
		<repository>
			<id>ui</id>
			<name>Human Readable Name for this Mirror.</name>
			<url>http://uk.maven.org/maven2/</url>
		</repository>
		<repository>
			<id>central</id>
			<url>http://repo1.maven.org/maven2</url>
		</repository>
		<repository>
			<id>clojars</id>
			<url>http://clojars.org/repo/</url>
		</repository>

		<repository>
			<id>ibiblio</id>
			<name>Human Readable Name for this Mirror.</name>
			<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
		</repository>
		<repository>
			<id>jboss-public-repository-group</id>
			<name>JBoss Public Repository Group</name>
			<url>http://repository.jboss.org/nexus/content/groups/public</url>
		</repository>
		<repository>
			<id>github-releases</id>
			<url>http://oss.sonatype.org/content/repositories/github-releases/</url>
		</repository>

		<repository>
			<id>com.snowplowanalytics</id>
			<name>SnowPlow Analytics</name>
			<url>http://maven.snplow.com/releases</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>

</project>


text:
import _ from "lodash";
import React from "react";

export const simulateClick = function(el) {
        // https://gomakethings.com/how-to-simulate-a-click-event-with-javascript/
        const evt = new MouseEvent("click", {
            bubbles: true,
            cancelable: true,
            view: window,
        });
        !el.dispatchEvent(evt);
    },
    randomString = function() {
        return "xxxxxxxxxxxxxxx".replace(/x/g, c =>
            String.fromCharCode(97 + parseInt(26 * Math.random()))
        );
    },
    getHeaders = function(csrfToken) {
        return {
            "X-CSRFToken": csrfToken,
            "Content-Type": "application/json",
        };
    },
    checkOrEmpty = bool => {
        // ✓/<null> in box
        return ;
    },
    checkOrTimes = bool => {
        // ✓/x in box
        return ;
    },
    getLabel = function(value, mapping) {
        return _.find(mapping, d => d.value == value).label;
    };


text:
layout: default
title: Home
---

I'm a hands-on creative producer with a strong creative background having previous experience as a writer and stylist. More

  {% for post in paginator.posts %}

      {% if post.img %}

      {% endif %}

        {{ post.title }}

  {% endfor %}


text:
`debops.log`_
=============

This is a `PlanetPlanet`_ feed aggregator configured to grab Atom feeds from
all DebOps repositories and merge them into 1 Atom/RSS feed to create
a project-wide changelog. We use this instead of the normal changelog to avoid
needless data duplication.

You can subscribe to the whole feed or to a particular Ansible role(s) using
your preferred RSS/Atom feed reader.

.. _debops.log: http://log.debops.org/
.. _PlanetPlanet: http://planetplanet.org/


text:
Use Mocha at Work?  Ask your manager or marketing team if they'd help [support](https://opencollective.com/mochajs#support) our project.  Your company's logo will also be displayed on [npmjs.com](http://npmjs.com/package/mocha) and our [GitHub repository](https://github.com/mochajs/mocha#sponsors).

<!-- markdownlint-disable MD034 -->
{% for i in (0..15) %}[![](https://opencollective.com/mochajs/sponsor/{{ i }}/avatar.jpg)](https://opencollective.com/mochajs/sponsor/{{ i }}/website){: target="_blank"} {% endfor %}
{: .image-list .faded-images}

<script src="js/avatars.js"></script>


text:
require 'spec_helper_acceptance'

describe 'concat backup parameter' do
  basedir = default.tmpdir('concat')
  context '=> puppet' do
    before(:all) do
      pp = <<-EOS
        file { '#{basedir}':
          ensure => directory,
        }
        file { '#{basedir}/file':
          content => "old contents\n",
        }
      EOS
      apply_manifest(pp)
    end
    pp = <<-EOS
      concat { '#{basedir}/file':
        backup => 'puppet',
      }
      concat::fragment { 'new file':
        target  => '#{basedir}/file',
        content => 'new contents',
      }
    EOS

    it 'applies the manifest twice with "Filebucketed" stdout and no stderr' do
      apply_manifest(pp, :catch_failures => true) do |r|
        expect(r.stdout).to match(/Filebucketed #{basedir}\/file to puppet with sum 0140c31db86293a1a1e080ce9b91305f/) # sum is for file contents of 'old contents'
      end
      apply_manifest(pp, :catch_changes => true)
    end

    describe file("#{basedir}/file") do
      it { should be_file }
      its(:content) { should match /new contents/ }
    end
  end

  context '=> .backup' do
    before(:all) do
      pp = <<-EOS
        file { '#{basedir}':
          ensure => directory,
        }
        file { '#{basedir}/file':
          content => "old contents\n",
        }
      EOS
      apply_manifest(pp)
    end
    pp = <<-EOS
      concat { '#{basedir}/file':
        backup => '.backup',
      }
      concat::fragment { 'new file':
        target  => '#{basedir}/file',
        content => 'new contents',
      }
    EOS

    # XXX Puppet doesn't mention anything about filebucketing with a given
    # extension like .backup
    it 'applies the manifest twice  no stderr' do
      apply_manifest(pp, :catch_failures => true)
      apply_manifest(pp, :catch_changes => true)
    end

    describe file("#{basedir}/file") do
      it { should be_file }
      its(:content) { should match /new contents/ }
    end
    describe file("#{basedir}/file.backup") do
      it { should be_file }
      its(:content) { should match /old contents/ }
    end
  end

  # XXX The backup parameter uses validate_string() and thus can't be the
  # boolean false value, but the string 'false' has the same effect in Puppet 3
  context "=> 'false'" do
    before(:all) do
      pp = <<-EOS
        file { '#{basedir}':
          ensure => directory,
        }
        file { '#{basedir}/file':
          content => "old contents\n",
        }
      EOS
      apply_manifest(pp)
    end
    pp = <<-EOS
      concat { '#{basedir}/file':
        backup => '.backup',
      }
      concat::fragment { 'new file':
        target  => '#{basedir}/file',
        content => 'new contents',
      }
    EOS

    it 'applies the manifest twice with no "Filebucketed" stdout and no stderr' do
      apply_manifest(pp, :catch_failures => true) do |r|
        expect(r.stdout).to_not match(/Filebucketed/)
      end
      apply_manifest(pp, :catch_changes => true)
    end

    describe file("#{basedir}/file") do
      it { should be_file }
      its(:content) { should match /new contents/ }
    end
  end
end


text:
Timelapse
=========
A Python script to take a series of pictures.

Timelapse is a simple script that takes a series of pictures with a
USB-connected, gphoto2-compatible camera.

Photos are stored on camera.

Command-line Arguments
----------------------
    -d, --delay: Time, in seconds, to wait between gphoto2 calls. Note that this
        is not the time between photographs! Rather it is the time paused
        between the end of a gphoto2 call, and the beginning of the next gphoto2
        call. So the time between photographs will be greater than this delay.
        By how much depends on the attached camera.

    -e --end: End date and time of the photo session. The argument is a string
        formatted in one of the following ways:
            <year>-<month>-<day> <hour>:<minute>:<second>
            <year>-<month>-<day> <hour>:<minute>
            <month>-<day> <hour>:<minute>:<second>
            <month>-<day> <hour>:<minute>
            <hour>:<minute>:<second>
            <hour>:<minute>

    Where:
        <year> is a four-digit year.
        <month> is a two-digit month, with a leading zero if necessary.
        <day> is a two-digit day, with a leading zero if necessary.
        <hour> is a 24-hour clock, two-digit hour, with a leading zero if
            necessary.
        <minute> is a two-digit minute, with a leading zero if necessary.
        <second> is a two-digit second, with a leading zero if necessary.

Requirements
------------
    * Python 3.x. (Tested with Python 3.5)
    * Gphoto2

Copyright and License
---------------------

Copyright 2015, Andrew Lin.
All rights reserved.

This software is released under the BSD 3-clause license. See LICENSE.txt or
https://opensource.org/licenses/BSD-3-Clause for more information